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

前端 未结 2 757
终归单人心
终归单人心 2020-12-17 18:45

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

相关标签:
2条回答
  • 2020-12-17 19:13

    You can use the Rect.IntersectsWith method

    0 讨论(0)
  • 2020-12-17 19:20

    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.

    0 讨论(0)
提交回复
热议问题