フォーカスが当たった時にBackgroundが変わるTextBoxがXamlだけで作れるWPFずるい。
WPF、フォーカスが当たった時にBackgroundが変わるTextBoxがXamlだけで作れるなんてずるい。
Triggerクラス
をつかえばちょいちょいなんだけど、そんなのSilverlightにはなかった。
(EventTrigger クラスというのはあるけどLoadedしか使えないし、Triggerクラスみたいな書き方はできない)
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Window.Resources> <Style x:Key="HighlightTextBox" TargetType="TextBox"> <!--StyleにTrriggerを定義--> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <!--IsFocusedがTrueの場合、下の値を適用--> <Setter Property="Background" Value="Yellow" /> </Trigger> <Trigger Property="IsFocused" Value="False"> <Setter Property="Background" Value="White" /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <TextBox Height="24" HorizontalAlignment="Left" Margin="30,30,0,0" VerticalAlignment="Top" Width="120" /> <!--↓フォーカスが当たると黄色くなるTextBox↓--> <TextBox Style="{StaticResource HighlightTextBox}" Height="24" HorizontalAlignment="Left" Margin="30,60,0,0" VerticalAlignment="Top" Width="120" /> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="30,90,0,30" Name="button1" VerticalAlignment="Top" Width="75" /> </Grid> </Window>
うーん。これは楽だ。WPFずるい。