问题与WPF焦点 [英] Issue with WPF Focus

查看:167
本文介绍了问题与WPF焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理我的控件的LostFocus事件的代码。它验证的价值,并在某些情况下,将启用后续控制。例如,可能有一个组合框允许用户选择一个国家。随后的ComboBox允许用户选择一个状态。如果当前选择的国家不是美国,则状态组合框被禁用。如果用户选择美国,然后退出组合框,LostFocus代码启用状态组合框。但是,状态ComboBox不会获得焦点,而是焦点转到状态组合框后面的控件。



我尝试使用PreviewLostKeyboardFocus来处理事件,而不是没有运气。我有点不知所措,想要破解WPF来完成这项工作。有什么建议吗?解决方案

尝试验证数据更改的时间,而不是UI。您可以添加将从绑定更新属性时触发的验证规则。然后,您可以使用样式触发器来激活有问题的控件。
检查这篇文章应该有所帮助。

I have code that handles the LostFocus event of my controls. It validates the value and in some cases will enable a subsequent control. For instance, there might be a ComboBox that allows a user to select a country. The subsequent ComboBox allows the user to select a state. IF the currently selected country is not the USA, the state ComboBox is disabled. If the user selects "USA" and then tabs out of the combo box, the LostFocus code enables the state ComboBox. However, the State ComboBox does not get focus, instead focus goes to the control that follows the State ComboBox.

I've tried using the PreviewLostKeyboardFocus to handle the event instead with no luck. I'm kind of at a loss as to figuring out someway to hack WPF to get this work. Any suggestions?

解决方案

try validating when the data changes, not the UI. You can add validation rules that will fire when the property is updated from the binding. Then you can use a style trigger to activate the control in question. Check this article it should help.

这篇关于问题与WPF焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆