附加命令行为和 LostFocus

发布于 2024-11-29 20:20:24 字数 904 浏览 0 评论 0原文

我正在使用此处描述的方法通过设置将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-12-06 20:20:25

您需要检查 LostFocus 事件:

LostFocus 事件是一个冒泡事件。这意味着如果多个
LostFocus 事件处理程序是为一系列对象注册的
通过对象树中的父子关系连接,事件
被该关系中的每个对象接收。冒泡的比喻
表示事件从直接接收的对象开始
输入条件,并沿着对象树向上移动。对于一个
冒泡事件,事件处理程序可用的发送者标识
处理事件的对象,不一定是处理事件的对象
实际上收到了启动事件的输入条件。要得到
发起事件的对象,使用 OriginalSource 值
事件的 RoutedEventArgs 事件数据。

因此,对于 ComboBox,您可能会收到 ComboBox 内各种可聚焦元素的事件。

You would need to check the OriginalSource of the event arguments for the LostFocus event:

The LostFocus event is a bubbling event. This means that if multiple
LostFocus event handlers are registered for a sequence of objects
connected by parent-child relationships in the object tree, the event
is received by each object in that relationship. The bubbling metaphor
indicates that the event starts at the object that directly receives
the input condition, and works its way up the object tree. For a
bubbling event, the sender available to the event handler identifies
the object where the event is handled, not necessarily the object that
actually received the input condition that initiated the event. To get
the object that initiated the event, use the OriginalSource value of
the event's RoutedEventArgs event data.

So for the ComboBox, you may receive events for the various focusable elements inside the ComboBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文