附加命令行为和 LostFocus
我正在使用此处描述的方法通过设置将 ViewModel ICommand 附加到组合框的 LostFocus 事件CommandBehavior.RoulatedEventName="LostFocus"
。我预计该事件会在 UpdateSourceTrigger=LostFocus
绑定触发的同时触发,但事实证明并非如此。
每当键盘跳开时,或者在用户通过单击实际从下拉列表中选择一个项目之后,selecteditem Binding UpdateSourceTrigger=LostFocus
就会触发(不确定为什么这会导致失去焦点,但至少在选择后它会触发)制成)。
只要用户单击组合框,就会触发附加的行为事件。立即地。如果使用键盘,它会正常工作,当您离开键盘时会触发。然而,当使用鼠标时,当控件获得焦点时,甚至在用户做出选择之前,该事件就会触发。有什么方法可以使其表现得像失去焦点对所选项目所做的那样?
编辑:我很好奇是否存在另一个答案,但我找到了解决此问题的方法,即设置额外的绑定。 SelectedItem 默认更新,处理正常的属性更改通知,并在丢失焦点时更新 selectedvalue,仅处理我尝试运行的命令。绑定看起来像这样:
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
SelectedValuePath="CM_CUSTOMER_ID"
SelectedValue="{Binding Path=CustomerLostFocus, UpdateSourceTrigger=LostFocus}"
I am using the method described here to attach a ViewModel ICommand to the LostFocus event of a Combobox, by setting CommandBehavior.RoutedEventName="LostFocus"
. I expected the event to fire at the same time the binding for UpdateSourceTrigger=LostFocus
fired, but this turns out not to be the case.
The selecteditem Binding UpdateSourceTrigger=LostFocus
fires whenever the keyboard tabs away, or after the user actually selects an item from the dropdown by clicking (not sure why this causes lostfocus, but at least it fires AFTER a selection is made).
The attached behavior event fires anytime the user clicks on the Combobox. Immediately. If using the keyboard it behaves normally, firing when you tab away from it. However, when using the mouse, the event fires when the control GAINS focus, before the user has even made a selection. Is there any way to make this behave like lostfocus does for the selecteditem?
Edit: I am curious if another answer exists, but I found a way around this problem, by setting up an additional binding. SelectedItem updates by defualt, handling the normal property change notifications, and selectedvalue updates on lostfocus, handling only the command I was trying to run. Binding looks like this:
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
SelectedValuePath="CM_CUSTOMER_ID"
SelectedValue="{Binding Path=CustomerLostFocus, UpdateSourceTrigger=LostFocus}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要检查 LostFocus 事件:
因此,对于 ComboBox,您可能会收到 ComboBox 内各种可聚焦元素的事件。
You would need to check the OriginalSource of the event arguments for the LostFocus event:
So for the ComboBox, you may receive events for the various focusable elements inside the ComboBox.