How to get control with lower zindex when mouse clicked in wpf?

江枫思渺然 提交于 2019-12-02 01:03:27

问题


I've several controls in the same canvas and may one be covered by another. They are all with same zIndex, but for the order loaded, some being up and others down. My Question if Acontrol is over Bcontrol, and I click on them, but only A gets the click event. How can I make B get the event, too? Thanks.


回答1:


If you only wanted the one in the back to get the event, then for all the controls in front of the first one, you have to set IsHitTestVisible = False for the one behind to get the event - but this isn't what you want.

If you want them all to get the event think of the entire UI as a tree of elements. All of these controls you're talking about are siblings. When something is clicked, the parent is the first to get notified, and if it doesn't handle the click, it gets passed down to the visible child element of that parent at that mouse position, and so on until it's handled. Your only way to stop the child that gets clicked from handling the mouse click is to have the common parent of all the siblings handle the event first.

You will then have to do something clever in the parent's handler to invoke the click event of all child elements that can be found beneath the mouse - the problem is that whereas the framework used to do the hard work of determining which control was under the mouse, you will will now have to do that hard work.




回答2:


No chance. Even if you mark MouseClick as unhandled it will route to parent element (Canvas) not sibling. The only way is hit-testing. When the user click on Acontrol you should hit-test to determine whether another control is under it.

You must use hit-test with callbacks. This one allows you to skip Acontrol in order to find Bcontrol. If you find it, you can treat Bcontrol as clicked.



来源:https://stackoverflow.com/questions/10100994/how-to-get-control-with-lower-zindex-when-mouse-clicked-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!