Silverlight 列表框触发 MouseRightButtonDown,但不触发 MouseLeftButtonDown

发布于 2024-09-01 10:46:34 字数 535 浏览 4 评论 0原文

我在一个更大的项目中遇到了这个问题……所以我设置了一个“Testpoject”作为概念验证:

  • 新的 Silverlight 应用程序
  • 添加列表框
  • 用几个复选框填充列表框
  • 注册 listBox1_MouseLeftButtonDown
  • 注册 listBox1_MouseRightButtonDown

您将看到,listBox1_MouseLeftButtonDown任何情况下都不会发生火灾...... 然而 listBox1_MouseRightButtonDown 触发得很好。

我尝试使用从 ListBox 派生的自定义类并覆盖,假设 ListBox 类中的某些内容设置了 e.Handled = false,但这也没有改变行为。

关于为什么会发生这种情况以及如何解决的任何想法?

(此问题还会阻止“父”控件接收单击事件...因此事件传递被破坏)

:编辑: 我用解决方法解决了我的问题...所以不再需要答案。只是如果有人想弄清楚为什么会发生这种情况;)

I have this problem in a bigger Project...... so I set up a 'Testpoject' as Proof of Concept:

  • New Silverlight-Application
  • Add Listbox
  • Fill listbox with a few Checkboxes
  • Register listBox1_MouseLeftButtonDown
  • register listBox1_MouseRightButtonDown

You will see, that the listBox1_MouseLeftButtonDown won't fire under any circumstances....
listBox1_MouseRightButtonDown however fires just fine.

I tried using a custom Class deriving from ListBox and overriding, assuming something in the ListBox Class was setting e.Handled = false, but this did not change the behaviour, either.

Any Ideas on why this happens and how to fix?

(This problem also stops the 'parent'-control from receiving the Click-Event... so the Event-passing is broke)

:edit:
I fixed my problem with a workaround... so an answer is not required anymore. Just if somebody feels like figuring out why this is happening for the sake of it ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

飘过的浮云 2024-09-08 10:46:34

似乎回答了您的问题。引用:

这是因为 ListBoxItem 在内部处理此事件以及 MouseLeftButtonDown 事件(停止冒泡)以实现项目选择。

解决方案是在代码隐藏文件中添加事件处理程序。来自文章:

尽管在路由事件处理程序中将 RoutedEventArgs 参数的 Handled 属性设置为 true 似乎会停止隧道传输或冒泡,但树上或下的各个处理程序仍然可以选择接收事件!这只能从过程代码中完成,使用 AddHandler 的重载来添加布尔型handledEventsToo参数。

不过请参阅最后的警告。

This seems to answer your question. To quote:

That's because ListBoxItem internally handles this event as well as the MouseLeftButtonDown event (halting the bubbling) to implement item selection.

The solution is to add the event handler in the code-behind file. From the article:

Although setting the RoutedEventArgs parameter's Handled property to true in a routed event handler appears to stop the tunneling or bubbling, individual handlers further up or down the tree can opt to receive the events anyway! This can only be done from procedural code, using an overload of AddHandler that adds a Boolean handledEventsToo parameter.

See the caveat at the end though.

旧伤还要旧人安 2024-09-08 10:46:34

这是设计使然。如果检查框架代码,您将看到 ListBoxItem 将 Handled 属性设置为 true。

我遇到了同样的问题,因此在 ListBoxItem.ItemTemplate 中,我在内容中添加了事件处理程序。

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
        ... other controls ...
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

This is by design. If you check the framework code, you'll see that the ListBoxItem sets the Handled property to true.

I had this same exact problem, so in my ListBoxItem.ItemTemplate, I added the event handler in my content.

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
        ... other controls ...
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文