How do I make an EventSetter in the App.xaml file in WPF?

2.4k Views Asked by At

I want every TextBox in my application to select all its text when the user focuses on it. To do this, I put the following in my App.xaml file:

<Application.Resources>
    <Style TargetType="TextBox" x:Key="tbkey">
        <EventSetter Event="GotFocus" Handler="textBoxFocus"/>
    </Style>
</Application.Resources>

and the following code in the App.xaml.cs file:

private void textBoxFocus(object sender, RoutedEventArgs a)
    {
        TextBox t = sender as TextBox;
        t.SelectAll();
    }

However, the method is never called when a TextBox is focused in my application. I think it is because I am not putting the handler method in the right location but I have no idea where that would be. Any ideas?

1

There are 1 best solutions below

2
On

Remove x:Key="tbkey" and your textBoxFocus method will be fired.

EDIT

Source Code can be downloaded here