简体   繁体   English

在两个用户控件上触发鼠标事件

[英]fire mouse events on both usercontrols

The thing is, I have 2 usercontrols, lets call them A and B. They both have MouseRightButtonDown and MouseRightButtonUp events and usercontrol A kinda overlaps B. Now when I right mouse click on A, the mouse event on B does not fire. 关键是,我有2个用户控件,分别称为A和B。它们都具有MouseRightButtonDown和MouseRightButtonUp事件,而usercontrol A则与B重叠。现在,当我右键单击A时,不会触发B上的鼠标事件。 When I disable the mouseevents on usercontrol A, the mouseevents on usercontrol B fires. 当我禁用用户控件A上的mouseevents时,将触发用户控件B上的mouseevents。

But how can I get them both to fire simultaneously? 但是我怎样才能让它们同时开火呢?

(hope I've explained it clearly) (希望我已经清楚地解释了)

a bit hacky but this would work, bind the event handler only to the control 1 and call the other event handler like this: 有点黑,但这可以工作,将事件处理程序仅绑定到控件1并调用其他事件处理程序,如下所示:

private void textBlock1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    Debug.WriteLine("textBlock1_MouseRightButtonDown");

    textBlock2_MouseRightButtonDown(sender, e);
}

private void textBlock2_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    Debug.WriteLine("textBlock2_MouseRightButtonDown");
}

personally I would not do this, I would do all my best to re-architect the logic and not have to call the other handler from one of the two controls, but without knowing more what your are doing, impossible to tell... 我个人不会这样做,我会尽力重新构造逻辑,而不必从两个控件中的一个调用另一个处理程序,但是如果不知道自己在做什么,就无法告诉...

As we are talking about Silverlight here, I strongly suggest to look on how to work Routed Events , which is a mechanism which actually will help you to avoid event relaunching, as these are events that are traverse Visual Tree of your element, from top to bottom. 当我们在这里谈论Silverlight时,我强烈建议您看一下如何处理Routed Events ,它实际上是一种机制,可以帮助您避免重新启动事件,因为这些事件从头到尾遍历了元素的Visual Tree。底部。

Definitely better then relaunching events. 绝对比重新发布事件更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM