在当前usercontrol上层嵌套UserControl里找到找到对应的上级控件操作

就是当前操作的UserControl 的上一级容器,然后以这个容器为突破口来找到当前的容器所在的UserControl,方法如下。

           WrapPanel ThisPanel = this.VisualParent as WrapPanel;
            if (ThisPanel.Parent!=null)
            {
                //UserControl GetMainWindows = WindowsOp.FindFirstUserControlParent(ThisPanel);//顶级windows
                UserControl GetMainWindows = WindowsOp.FindVisualParent<UserControl>(ThisPanel);//顶级windows
                

                WinDengjiList ThisWin = GetMainWindows as WinDengjiList;
                ThisWin.BindList(ThisFahui.Id);
                
            }

具体试实现的方法:

 /// <summary>
        /// 获取的上级第一个符合条件的控件
        /// </summary>
        /// <typeparam name="T">控件类型</typeparam>
        /// <param name="child">当前控件(获取他的上级控件)</param>
        /// <returns></returns>

        public static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            // Get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            // We've reached the end of the tree
            if (parentObject == null) return null;

            // If the parent is the right type, cast and return it
            T parent = parentObject as T;//也可以用这个方法来确定是否当前控件类型 parentObject.DependencyObjectType.BaseType.Name== "UserControl"
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // Otherwise, recursively continue to walk up the tree
                return FindVisualParent<T>(parentObject);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值