wpf 如何对 XAML 中定义的矩形使用 IntersectsWith 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17635336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:12:54  来源:igfitidea点击:

How to use IntersectsWith method with a rectangle defined in XAML

wpfxamlhittestrectangles

提问by Micha?l Polla

I have this rectangle in XAML :

我在 XAML 中有这个矩形:

<Rectangle x:Name="MyRectangle" Height="300" Width="300"></Rectangle>

I want to check if it intersects with another rectangle. In this question on SO, they say that one have to use the IntersectsWith method. But I'm unable to use it in code-behind. When I write in C# :

我想检查它是否与另一个矩形相交。在关于 SO 的这个问题中,他们说必须使用IntersectsWith 方法。但我无法在代码隐藏中使用它。当我用 C# 编写时:

MyRectangle.IntersectsWith(

I get the standard error:

我得到标准错误:

"System.Windows.Shapes.Rectangle does not contain a definition for 'IntersectsWith' and no extension method [...]"

“System.Windows.Shapes.Rectangle 不包含 'IntersectsWith' 的定义,也没有扩展方法 [...]”

I think that's because the rectangle in XAML is a System.Windows.Shapes.Rectangle, and the method is for System.Windows.Rect? If so, is there a way to "transform" my Rectangleinto a Rect?

我认为这是因为 XAML 中的矩形是 a System.Windows.Shapes.Rectangle,而该方法用于System.Windows.Rect? 如果是这样,有没有办法将 myRectangle转换为 a Rect

采纳答案by Micha?l Polla

Here's the solution I finally used. For each element I want to test if it intersects with others, I create a Rect containing it. Thus, I can use the IntersectsWith method.

这是我最终使用的解决方案。对于我想测试它是否与其他元素相交的每个元素,我创建了一个包含它的 Rect。因此,我可以使用 IntersectsWith 方法。

Example (with rectangles, but you can do this with other figures, UserControls,...) : XAML

示例(使用矩形,但您可以使用其他图形、用户控件、...):XAML

<Canvas>
    <Rectangle x:Name="Rectangle1" Height="100" Width="100"/>
    <Rectangle x:Name="Rectangle2" Height="100" Width="100" Canvas.Left="50"/>
</Canvas>

C#

C#

Rect rect1 = new Rect(Canvas.GetLeft(Rectangle1), Canvas.GetTop(Rectangle1), Rectangle1.Width, Rectangle1.Height);
Rect rect2 = new Rect(Canvas.GetLeft(Rectangle2), Canvas.GetTop(Rectangle2), Rectangle2.Width, Rectangle2.Height);
if(rect1.IntersectsWith(r2))
{
    // The two elements overlap
}

回答by Nitesh

Try it

尝试一下

MyRectangle.RenderedGeometry.Bounds.IntersectsWith();

回答by makc

you can use VisualTreeHelper.HitTestto test intersection don`t forget to set GeometryHitTestParameters

您可以使用VisualTreeHelper.HitTest来测试交叉点不要忘记设置GeometryHitTestParameters

Windows Presentation Foundation (WPF) hit testing only considers the filled area of a geometry during a hit test. If you create a point Geometry, the hit test would not intersect anything because a point has no area.

Windows Presentation Foundation (WPF) hit testing only considers the filled area of a geometry during a hit test. If you create a point Geometry, the hit test would not intersect anything because a point has no area.