none
如何使窗体内部的或者一个布局容器内的控件不可被点击 RRS feed

  • 问题

  • 如题,我要使一个窗体的内部的所有元素或者一个布局容器内(Grid,stackPanel,DOckPanel这类)的所有元素不可被点击,至少任意鼠标事件不会被响应。怎么做?
    2018年9月12日 3:23

答案

  • Hi  便携式家园,

    >>如题,我要使一个窗体的内部的所有元素或者一个布局容器内(Grid,stackPanel,DOckPanel这类)的所有元素不可被点击,至少任意鼠标事件不会被响应。怎么做?

    你可以尝试使用IsHitTestVisible 这个属性。将值设置为False,可以禁止返回命中结果。

     <Grid x:Name="MainGrid"   Loaded="MainGrid_Loaded" IsHitTestVisible="False">

    或者可以针对每个子控件做一个遍历。

                foreach (UIElement uie in FindVisualChildren<UIElement>(MainGrid))
                {
                    uie.IsHitTestVisible = false;   
                }
    
    
            public static 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;
                        }
                    }
                }
            }
    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年9月13日 7:47
    版主