用户控制处焦点丢失 [wpf]
FocusLost 事件在 WPF 组件上正常工作 对于用户控件来说,情况有所不同:
如果单击或聚焦用户控件中的任何控件,则立即触发用户控件的 FocusLost !我怎样才能阻止它呢?
我无法解决这个问题:(
FocusLost event is working on WPF's components properly
For User-Controls it is different:
if any control in the User-Control is clicked or focused, the User-Control's FocusLost is fired immediately ! How can I hinder it ?
I could not solve this problem :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查
UserControl
上的IsKeyboardFocusWithin
来确定UserControl
或其任何子项是否具有KeyBoard 焦点。您还可以使用事件IsKeyboardFocusWithinChanged
,只需检查e.OldValue
是否为true
和e.NewValue
> 在事件处理程序中为false
。编辑。
下面是一个示例,说明当
IsKeyboardFocusWithin
变为 false 时,如何使UserControl
引发路由事件 (UserControlLostFocus
)。一样使用
像示例
UserControl
You can check
IsKeyboardFocusWithin
on theUserControl
to determine if theUserControl
or any of its children has the KeyBoard focus or not. There is also the eventIsKeyboardFocusWithinChanged
which you can use, just check ife.OldValue
istrue
ande.NewValue
isfalse
in the event handler.Edit.
Here is an example of how you could make the
UserControl
raise a routed event (UserControlLostFocus
) whenIsKeyboardFocusWithin
turns to false.Useable like
Sample
UserControl
如果您不希望在焦点丢失时触发任何内容,可以将其添加到 XAML 用户控件后面的源 .CS 文件中:
If you dont want anything to fire on focus being lost you can add this to the source .CS file behind the XAML User Control: