How can i check if 2 controls overlap eachother on a canvas in WPF?

ε祈祈猫儿з 提交于 2019-11-27 23:50:32

问题


I am writing a designer that enables the user to drag controls around the screen. What would be the best way of detecting if a control is overlapping another control while i am dragging the one control around?

Should i just get the dimensions of the FrameworkElement and keep on checking the dimensions of the other elements?

Thanks. Eli


回答1:


The dimension (FrameworkElement.ActualWidth FrameworkElement.ActualHeight) and postion (Canvas.Top, Canvas.Bottom,Canvas.Left, Canvas.Right) of your elements would suffice if they are always rectangular. In that case you can easily calculate if two rectangles overlap. If you elements can be of more complex shapes it gets hairy. I have no idea if I can test for intersection of two Visual instances in WPF.

Maybe we can use hittesting? With this approach you could at least test if a certain point or geometry intersect with a certian visual. So you would have to define a geometry or a list of points that more or less closely match the bounds of your 2nd visual.




回答2:


You can use the Rect.IntersectsWith method



来源:https://stackoverflow.com/questions/1554116/how-can-i-check-if-2-controls-overlap-eachother-on-a-canvas-in-wpf

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