树形ContainerFromItem始终返回null [英] Treeview ContainerFromItem always returns null

查看:521
本文介绍了树形ContainerFromItem始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于这个问题的几个线程,但找不到什么做什么,我试图做的。我有一个绑定到一组分层对象的树视图。每个对象的表示在地图上的图标。当用户点击地图上的一个图标,我要选择树视图中的项目,专注于它,它滚动到视图。该地图对象绑定到TreeView对象的列表。在这个例子中,事情是绑定到树对象的类型。

I've read a few threads on this subject but couldn't find anything to do what I'm trying to do. I have a treeview that is bound to a hierarchical set of objects. Each of these objects represents an icon on a map. When the user clicks one of the icons on the map, I want to select the item in the tree view, focus on it, and scroll it into view. The map object has a list of the objects that are bound to the treeview. In the example, Thing is the type of object bound to the tree.

public void ScrollIntoView(Thing t)
{
  if (t != null)
  {
    t.IsSelected = true;
    t.IsExpanded = true;

    TreeViewItem container = (TreeViewItem)(masterTreeView
      .ItemContainerGenerator.ContainerFromItem(t));
    if (container != null)
    {
      container.Focus();
      LogicalTreeHelper.BringIntoView(container);
    }
  }
}



到目前为止,不管是什么我尝试过,容器始终为空。任何想法?

So far, no matter what I've tried, container is always null. Any ideas?

推荐答案

实际上是项目的子 masterTreeView

这实际上可能是相当困难的,因为 TreeViewItems ItemsControls 用自己的 ItemContainerGenerator 这意味着你应该只能够获得从容器中直接父的 ItemContainerGenerator 而不是从根。

This might actually be quite difficult since TreeViewItems are ItemsControls with their own ItemContainerGenerator which means you should only be able to get the container from the immediate parent's ItemContainerGenerator and not from the root.

有些递归函数,第一层次上升到根,然后反转在UI层面始终得到物品的容器可能会制定出这条路线但您的数据项需要它们的逻辑父数据对象的引用。

Some recursive function which first goes up the hierarchy to the root and then reverses this route on the ui level always getting the container of the items might work out but your data-items need a reference to their logical parent data object.

这篇关于树形ContainerFromItem始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆