加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF UIElement.IsHitTestVisible = false;仍然返回命中?

发布时间:2020-12-15 06:24:46 所属栏目:百科 来源:网络整理
导读:我从FrameworkElement派生一个控件,用作VisualCollection的容器,因为我正在使用DrawingVisuals(创建游戏图)进行大量的自定义渲染. 我有几个不同的我的容器的实例分层在彼此之上,我只想要命中测试影响当前可见的层,所以我尝试做明显的,并设置.IsHitTestVisibl
我从FrameworkElement派生一个控件,用作VisualCollection的容器,因为我正在使用DrawingVisuals(创建游戏图)进行大量的自定义渲染.

我有几个不同的我的容器的实例分层在彼此之上,我只想要命中测试影响当前可见的层,所以我尝试做明显的,并设置.IsHitTestVisible = false,根据MSDN应该防止任何子元素作为命中结果返回.

但是,我仍然在设置的容器上返回命中.IsHitTestVisible = false.
我已经尝试过一切我可以想到的,折叠,隐藏,残疾,0不透明度,似乎没有把它从命中测试.

解决方法

我认为这是一个bug.我使用Reflector来了解为什么HitTest方法返回不可见的项目,我发现没有检查可见性.

我的解决方案是使用过滤器的HitTest:

public static HitTestFilterBehavior HitTestFilterInvisible(DependencyObject potentialHitTestTarget)
{
    bool isVisible = false;
    bool isHitTestVisible = false;

    var uiElement = potentialHitTestTarget as UIElement;
    if (uiElement != null)
    {
        isVisible = uiElement.IsVisible;
        if (isVisible)
        {
            isHitTestVisible = uiElement.IsHitTestVisible;
        }
    }
    else
    {
        UIElement3D uiElement3D = potentialHitTestTarget as UIElement3D;
        if (uiElement3D != null)
        {
            isVisible = uiElement3D.IsVisible;
            if (isVisible)
            {
                isHitTestVisible = uiElement3D.IsHitTestVisible;
            }
        }
    }

    if (isVisible)
    {
        return isHitTestVisible ? HitTestFilterBehavior.Continue : HitTestFilterBehavior.ContinueSkipSelf;
    }

    return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
}
...
// usage:

    VisualTreeHelper.HitTest(
        myHitTestReference,HitTestFilterInvisible,hitTestResult =>
        {
            // code to handle element which is visible to the user and enabled for hit testing.
        },new PointHitTestParameters(myHitTestPoint));

我希望它能帮助你

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读