-2

UserControl在另一个中有一个“孩子” UserControl(在 a 中充当 TabItem TabControl)。在子UserControl元素和 TabItem 祖先之间是许多其他控件(例如:Grids、a StackPanel、可能是 aScrollViewer等)。

我想UserControl在我的孩子中访问 TabItem 的一个属性,UserControl并自定义一个通常 建议的递归函数,该函数沿着 Visual 树向上走。但是,这总是true在第一次空检查时返回,直到我在逻辑树上添加查询。

代码:

public MyTabItem FindParentTabItem(DependencyObject child)
{
  DependencyObject parent = VisualTreeHelper.GetParent(child) ?? LogicalTreeHelper.GetParent(child);

  // are we at the top of the tree
  if (parent == null)
  {
      return null;
  }
  MyTabItem parentTabItem = parent as MyTabItem;
  if (parentTabItem != null)
  {
    return parentTabItem;
  }
  else
  {
    //use recursion until it reaches the control
    return FindParentTabItem(parent);
  }
}

不幸的是,这也返回 null。当单步执行该方法时,我看到它确实找到了正确的UserControlTabItem,但是当它通过返回递归(?)返回时,它将它恢复为 null,然后返回给调用方法(在 childUserControl的 Loaded 事件中):

MyTabItem tab = FindParentTabItem(this);

如何解决此问题,以便我的方法正确返回 found MyTabItem

4

1 回答 1

0

这是一个有效的单元测试解决方案。

public static T FindAncestor<T>(DependencyObject obj)
    where T : DependencyObject
{
    if (obj != null)
    {
        var dependObj = obj;
        do
        {
            dependObj = GetParent(dependObj);
            if (dependObj is T)
                return dependObj as T;
        }
        while (dependObj != null);
    }

    return null;
}

public static DependencyObject GetParent(DependencyObject obj)
{
    if (obj == null)
        return null;
    if (obj is ContentElement)
    {
        var parent = ContentOperations.GetParent(obj as ContentElement);
        if (parent != null)
            return parent;
        if (obj is FrameworkContentElement)
            return (obj as FrameworkContentElement).Parent;
        return null;
    }

    return VisualTreeHelper.GetParent(obj);
}

用法是

FindAncestor<MyTabItemType>(someChild);

编辑:

假设您的 xaml 看起来像您描述的那样:

<UserControl>
    <Grid></Grid>
    <StackPanel></StackPanel>
    <!-- Probably also something around your child -->
    <Grid>
        <UserControl x:Name="child"/>
    </Grid>
</UserControl>

您目前在您的 child-xaml.cs

void OnChildUserControlLoaded(object sender, RoutedEventArgs e)
{
    var parent = FindAncestor<ParentUserControlType>(this);
    DoSomething(parent.SomeProperty);
}

除非您执行未描述的操作,否则代码将按原样工作。
我建议您向MCVE提供所有必要的信息。

于 2018-11-08T05:49:58.143 回答