WPF style (Background)

Site Index
カーソルフォーカス位置の表示例



<Window.Resources>
<Style x:Key="xKey_FocusColor" TargetType="TextBox" x:Name="xName_SelectText">
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="Gold" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="False">
<Setter Property="Background" Value="Beige" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>




<TextBox Grid.Column="6" Grid.Row="3" Margin="3" Style="{StaticResource xKey_FocusColor}" Name="text3" />
<TextBox Grid.Column="6" Grid.Row="4" Margin="3" Style="{StaticResource xKey_FocusColor}" Name="text4" />
<TextBox Grid.Column="6" Grid.Row="5" Margin="3" Style="{StaticResource xKey_FocusColor}" Name="text5" />





Style w_style_focus = new Style(typeof(TextBox));
//
Trigger w_trigger_focusGet = new Trigger { Property = TextBox.IsKeyboardFocusedProperty, Value = true };
w_trigger_focusGet.Setters.Add(new Setter(TextBox.BackgroundProperty, new SolidColorBrush(Colors.Pink)));
w_style_focus.Triggers.Add(w_trigger_focusGet);
//
Trigger w_trigger_focusOut = new Trigger { Property = TextBox.IsKeyboardFocusedProperty, Value = false };
w_trigger_focusOut.Setters.Add(new Setter(TextBox.BackgroundProperty, new SolidColorBrush(Colors.LawnGreen)));
w_style_focus.Triggers.Add(w_trigger_focusOut);
//
z_textName01[idx].Style = w_style_focus;





//*************************************************
// TextBox01
//*************************************************
void z_TextCode01_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
TextBox w_textBox = (TextBox)e.Source;
int idx = idx_textCode01[w_textBox];
}
void z_TextCode01_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox w_textBox = sender as TextBox;
w_textBox.Background = new SolidColorBrush(Colors.Yellow);
}
void z_TextCode01_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox w_textBox = sender as TextBox;
w_textBox.Background = new SolidColorBrush(Colors.AliceBlue);
}
void z_TextCode01_PreviewKeyDown(object sender, KeyEventArgs e)
{
TextBox w_textBox = (TextBox)e.Source;
int idx = idx_textCode01[w_textBox];
if (e.Key == Key.Enter)
{
z_textCode02[idx].Focus();
}
if (e.Key == Key.Up)
{
if (idx > 1)
{
z_textCode01[idx - 1].Focus();
}
}
}




AX