読者です 読者をやめる 読者になる 読者になる

qshinoの日記

Powershell関係と徒然なこと

WPF背景色

wpf背景色

WPFでコントロールに背景色を付ける。

気合いでやるなら、このぐらい。

<Window x:Class="BackgroundColorChange.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="Keypad" Height="500" Width="500">
 <Grid Height="306" ButtonBase.Click="button_Click" UIElement.GotFocus="textBox1_GotFocus">
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="0" Background="AliceBlue">
   <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
     <Style.Triggers>
      <Trigger Property="IsFocused" Value="true">
       <strong><Setter Property="Background"></strong>
        <Setter.Value>
         <LinearGradientBrush>
          <GradientStop Color="Red"/>
         </LinearGradientBrush>
        </Setter.Value>
       </Setter>
      </Trigger>
     </Style.Triggers>
    </Style>
   </TextBox.Style>
  </TextBox>
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,56,0,0" Name="textBox2" Text="0" VerticalAlignment="Top" Width="120" Background="AliceBlue" />
  <TextBox Height="23" HorizontalAlignment="Left" Margin="24,85,0,0" Name="textBox3" Text="0" VerticalAlignment="Top" Width="120" Background="AliceBlue" />
  <Button Focusable="False" Content="1" Height="25" HorizontalAlignment="Left" Margin="303,24,0,0" Name="button2" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="2" Height="25" HorizontalAlignment="Left" Margin="303,55,0,0" Name="button5" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="3" Height="25" HorizontalAlignment="Left" Margin="303,86,0,0" Name="button8" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="4" Height="25" HorizontalAlignment="Left" Margin="351,24,0,0" Name="button1" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="5" Height="25" HorizontalAlignment="Left" Margin="351,55,0,0" Name="button3" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="6" Height="25" HorizontalAlignment="Left" Margin="351,86,0,0" Name="button4" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="7" Height="25" HorizontalAlignment="Left" Margin="401,24,0,0" Name="button7" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="8" Height="25" HorizontalAlignment="Left" Margin="401,55,0,0" Name="button9" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="9" Height="25" HorizontalAlignment="Left" Margin="401,86,0,0" Name="button10" VerticalAlignment="Top" Width="32" />
  <Button Focusable="False" Content="0" Height="25" HorizontalAlignment="Left" Margin="351,117,0,0" Name="button6" VerticalAlignment="Top" 

参考

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8c18880-f21c-40e5-a8bc-f1a9aaae7635/how-to-change-textbox-background-color-on-xaml?forum=wpf

http://stackoverflow.com/questions/1441059/changing-background-color-of-container-when-textbox-is-in-focus

http://stackoverflow.com/questions/15399622/accessing-background-color-of-textblock