WPF - 命中测试过滤器回调

发布于 2024-10-06 18:32:00 字数 1081 浏览 0 评论 0原文

我有一个画布,它的 VisualCollection 中有子 DrawingVisuals。我想对某种类型的孩子进行命中测试,但不想对其他类型的孩子进行命中测试。为此,我编写了 HitTestFilterCallback 函数:

public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
    Debug.WriteLine(o.GetType());
    if (o is BackgroundLine)
    {
        return HitTestFilterBehavior.ContinueSkipSelf;
    }
    else
    {
        return HitTestFilterBehavior.Continue;
    }
}

因此,我检查 canvas 的子元素是否是从 DrawingVisual 派生的 BackgroundLine,如果是,则跳过它。但是,我从 Debug.WriteLine(o.GetType()) 获得的类型只是 System.Windows.Media.DrawingVisual。有没有办法找到最具体的对象类型?

其余代码如下。我只想针对 GraphicsBase 对象进行测试。

GraphicsBase hit = null;
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    hit = (GraphicsBase)result.VisualHit;

    return HitTestResultBehavior.Stop;
}

    VisualTreeHelper.HitTest(drawingCanvas, new HitTestFilterCallback(MyHitTestFilter), 
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point));

if (hit != null)
    Debug.WriteLine("hit");
else
    Debug.WriteLine("nothing");

I have a canvas and it has child DrawingVisuals in its VisualCollection. I want to hit test against some type of child but not for others. To do that I wrote HitTestFilterCallback function:

public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
    Debug.WriteLine(o.GetType());
    if (o is BackgroundLine)
    {
        return HitTestFilterBehavior.ContinueSkipSelf;
    }
    else
    {
        return HitTestFilterBehavior.Continue;
    }
}

So I check whether the child of canvas is a BackgroundLine, which is derived from DrawingVisual, and if it is I skip it. However, the type I am getting from Debug.WriteLine(o.GetType()) is only System.Windows.Media.DrawingVisual. Is there a way I can find the most specific object type?

Rest of the code is below. I want to test against GraphicsBase objects only.

GraphicsBase hit = null;
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    hit = (GraphicsBase)result.VisualHit;

    return HitTestResultBehavior.Stop;
}

    VisualTreeHelper.HitTest(drawingCanvas, new HitTestFilterCallback(MyHitTestFilter), 
new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(point));

if (hit != null)
    Debug.WriteLine("hit");
else
    Debug.WriteLine("nothing");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-10-13 18:32:00

我发现了问题。我看到的 DrawingVisual 对象是我为背景颜色添加的矩形。我忘记了这一点,并认为我正在将 BackgroundLine 对象的类型设置为 DrawingVisual。正如 rooks 所说,我可以获得特定的 BackgroundLine 类型。谢谢。

I found the problem. The DrawingVisual object I am seeing was the rectangle I added for background color. I forgot about that and thought I was getting BackgroundLine object's type as DrawingVisual. I can get the specific BackgroundLine type as rooks said. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文