代码之家  ›  专栏  ›  技术社区  ›  Trap

在WPF中,如何确定控件是否对用户可见?

  •  59
  • Trap  · 技术社区  · 16 年前

    是否有任何方法可以确定控件是否对用户“可见”?

    注意:我已经在使用IsExpanded属性跳过更新折叠的节点,但是有些节点有100多个元素,无法找到跳过栅格视口之外的元素的方法。

    4 回复  |  直到 16 年前
        1
  •  88
  •   Adi Lester    14 年前

    您可以使用我刚刚编写的这个小助手函数来检查给定容器中的元素对用户是否可见。函数返回 true 如果元素部分可见。如果要检查它是否完全可见,请将最后一行替换为 rect.Contains(bounds) .

    private bool IsUserVisible(FrameworkElement element, FrameworkElement container)
    {
        if (!element.IsVisible)
            return false;
    
        Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
        Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
        return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
    }
    

    就你而言, element container 你的窗户。

        2
  •  19
  •   Vadim Ovchinnikov    8 年前
    public static bool IsUserVisible(this UIElement element)
    {
        if (!element.IsVisible)
            return false;
        var container = VisualTreeHelper.GetParent(element) as FrameworkElement;
        if (container == null) throw new ArgumentNullException("container");
    
        Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height));
        Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
        return rect.IntersectsWith(bounds);
    }
    
        3
  •  7
  •   Ofer Barasofsky    8 年前

    如何确定控件对用户是否可见 . 问题是 即使它可以被渲染,并且它在容器的边界内,这也是其他答案所要解决的。

    确定用户是否可以单击WPF UIElement(或在PC上可以访问鼠标)

    我在尝试检查用户是否可以用鼠标单击按钮时遇到了这个问题。让我感到困扰的一个特例是,一个按钮实际上对用户是可见的,但是被一些透明(或半透明或不透明)层覆盖,以防止鼠标点击。在这种情况下,控件可能对用户可见,但对用户不可访问,这有点像它根本不可见。

    所以我必须想出自己的解决办法。

    -我原来的帖子有一个不同的解决方案,它使用了InAuthitTest方法。然而,它在很多情况下都不起作用,我不得不重新设计它。这个解决方案更加健壮,并且似乎工作得非常好,没有任何误报或误报。

    解决方案:

    1. 获取相对于应用程序主窗口的对象绝对位置
    2. 呼叫 VisualTreeHelper.HitTest 在其所有角落(左上角、左下角、右上角、右下角)
    3. 我们称之为物体 完全可点击 VisualTreeHelper.HitTest 等于原始对象或其所有角的视觉父对象,以及 用于一个或多个角。

    请注意#1:此处定义为完全可点击或部分可点击 可点击并不精确-我们只是检查一个屏幕的所有四个角 对象是可单击的。例如,如果一个按钮有4个可点击的角,但它是 中心有一个不可点击的点,我们仍将其视为 完全可点击。如果要检查给定对象中的所有点,则太难了 浪费的

    请注意#2:有时需要设置对象 IsHitTestVisible 财产 符合事实的 (但是,这是许多常见问题的默认值。) 如果我们愿意的话 VisualTreeHelper.HitTest

        private bool isElementClickable<T>(UIElement container, UIElement element, out bool isPartiallyClickable)
        {
            isPartiallyClickable = false;
            Rect pos = GetAbsolutePlacement((FrameworkElement)container, (FrameworkElement)element);
            bool isTopLeftClickable = GetIsPointClickable<T>(container, element, new Point(pos.TopLeft.X + 1,pos.TopLeft.Y+1));
            bool isBottomLeftClickable = GetIsPointClickable<T>(container, element, new Point(pos.BottomLeft.X + 1, pos.BottomLeft.Y - 1));
            bool isTopRightClickable = GetIsPointClickable<T>(container, element, new Point(pos.TopRight.X - 1, pos.TopRight.Y + 1));
            bool isBottomRightClickable = GetIsPointClickable<T>(container, element, new Point(pos.BottomRight.X - 1, pos.BottomRight.Y - 1));
    
            if (isTopLeftClickable || isBottomLeftClickable || isTopRightClickable || isBottomRightClickable)
            {
                isPartiallyClickable = true;
            }
    
            return isTopLeftClickable && isBottomLeftClickable && isTopRightClickable && isBottomRightClickable; // return if element is fully clickable
        }
    
        private bool GetIsPointClickable<T>(UIElement container, UIElement element, Point p) 
        {
            DependencyObject hitTestResult = HitTest< T>(p, container);
            if (null != hitTestResult)
            {
                return isElementChildOfElement(element, hitTestResult);
            }
            return false;
        }               
    
        private DependencyObject HitTest<T>(Point p, UIElement container)
        {                       
            PointHitTestParameters parameter = new PointHitTestParameters(p);
            DependencyObject hitTestResult = null;
    
            HitTestResultCallback resultCallback = (result) =>
            {
               UIElement elemCandidateResult = result.VisualHit as UIElement;
                // result can be collapsed! Even though documentation indicates otherwise
                if (null != elemCandidateResult && elemCandidateResult.Visibility == Visibility.Visible) 
                {
                    hitTestResult = result.VisualHit;
                    return HitTestResultBehavior.Stop;
                }
    
                return HitTestResultBehavior.Continue;
            };
    
            HitTestFilterCallback filterCallBack = (potentialHitTestTarget) =>
            {
                if (potentialHitTestTarget is T)
                {
                    hitTestResult = potentialHitTestTarget;
                    return HitTestFilterBehavior.Stop;
                }
    
                return HitTestFilterBehavior.Continue;
            };
    
            VisualTreeHelper.HitTest(container, filterCallBack, resultCallback, parameter);
            return hitTestResult;
        }         
    
        private bool isElementChildOfElement(DependencyObject child, DependencyObject parent)
        {
            if (child.GetHashCode() == parent.GetHashCode())
                return true;
            IEnumerable<DependencyObject> elemList = FindVisualChildren<DependencyObject>((DependencyObject)parent);
            foreach (DependencyObject obj in elemList)
            {
                if (obj.GetHashCode() == child.GetHashCode())
                    return true;
            }
            return false;
        }
    
        private IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }
    
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    
        private Rect GetAbsolutePlacement(FrameworkElement container, FrameworkElement element, bool relativeToScreen = false)
        {
            var absolutePos = element.PointToScreen(new System.Windows.Point(0, 0));
            if (relativeToScreen)
            {
                return new Rect(absolutePos.X, absolutePos.Y, element.ActualWidth, element.ActualHeight);
           }
            var posMW = container.PointToScreen(new System.Windows.Point(0, 0));
            absolutePos = new System.Windows.Point(absolutePos.X - posMW.X, absolutePos.Y - posMW.Y);
            return new Rect(absolutePos.X, absolutePos.Y, element.ActualWidth, element.ActualHeight);
       }
    

    然后,要确定按钮(例如)是否可单击,只需调用:

     if (isElementClickable<Button>(Application.Current.MainWindow, myButton, out isPartiallyClickable))
     {
          // Whatever
     }
    
        4
  •  5
  •   Timoi    16 年前

    对包含控件使用以下属性:

    VirtualizingStackPanel.IsVirtualizing="True" 
    VirtualizingStackPanel.VirtualizationMode="Recycling"
    

        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                Console.WriteLine(
                   "WPF is listening my property changes so I must be visible");
            }
            remove
            {
                Console.WriteLine("WPF unsubscribed so I must be out of sight");
            }
        }
    

    有关详细信息,请参阅: http://joew.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3DWPF