开发者

fire mouse events on both usercontrols

开发者 https://www.devze.com 2023-04-07 00:25 出处:网络
The thing is, I have 2 usercontrols, lets call them A and B. They both have MouseRightButtonDown and MouseRightButtonUp events and us开发者_开发技巧ercontrol A kinda overlaps B.

The thing is, I have 2 usercontrols, lets call them A and B. They both have MouseRightButtonDown and MouseRightButtonUp events and us开发者_开发技巧ercontrol A kinda overlaps B. Now when I right mouse click on A, the mouse event on B does not fire. When I disable the mouseevents on usercontrol A, the mouseevents on usercontrol B fires.

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:

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.

Definitely better then relaunching events.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号