Reputation: 8778
On my WPF Window, I want the textboxes to have a slightly blue background when the cursor is on them. I created two simple event handlers (GotFocus and LostFocus) to do this.
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = (Brush)new BrushConverter().ConvertFrom("#FFE6E6FF");
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = Brushes.White;
}
Is there a way I can direct every textbox to one eventhandler that gives the background to the respective textbox?
Upvotes: 0
Views: 289
Reputation: 51369
Very simple. Put the event hook on the outermost container of all of the text boxes:
<Window TextBox.GotFocus="textBox1_GotFocus" TextBox.LostFocus="textBox1_LostFocus">
<TextBox ... >
<TextBox ... >
<TextBox ... >
</Window>
To make it operate on the correct textbox, cast the "sender" parameter as a textbox:
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).Background = (Brush)new BrushConverter().ConvertFrom("#FFE6E6FF");
}
Upvotes: 1