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?
Very simple. Put the event hook on the outermost container of all of the text boxes:
To make it operate on the correct textbox, cast the “sender” parameter as a textbox: