如何在 WPF 的 App.xaml 文件中创建 EventSetter?
我希望应用程序中的每个文本框在用户聚焦时选择其所有文本。为此,我将以下内容放入 App.xaml 文件中:
<Application.Resources>
<Style TargetType="TextBox" x:Key="tbkey">
<EventSetter Event="GotFocus" Handler="textBoxFocus"/>
</Style>
</Application.Resources>
并将以下代码放入 App.xaml.cs 文件中:
private void textBoxFocus(object sender, RoutedEventArgs a)
{
TextBox t = sender as TextBox;
t.SelectAll();
}
但是,当 TextBox 在我的应用程序中聚焦时,永远不会调用该方法。我认为这是因为我没有将处理程序方法放在正确的位置,但我不知道它会在哪里。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除 x:Key="tbkey" 并且您的 textBoxFocus 方法将被触发。
编辑
Remove x:Key="tbkey" and your textBoxFocus method will be fired.
EDIT