就是当前操作的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);
}
}

2127

被折叠的 条评论
为什么被折叠?



