开发者

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

开发者 https://www.devze.com 2023-02-25 05:30 出处:网络
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:

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?


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

EDIT

Source Code can be downloaded here

0

精彩评论

暂无评论...
验证码 换一张
取 消