我正在使用 TreeView 和 HierarchicalDataTemplate 来显示从 web 服务返回的分层列表。根据搜索条件,此列表可能会变得很长,并且有几个嵌套级别。向用户显示各种“地图”会很有用,这样他们就可以在这个列表中看到他们相对于顶层的位置。用于创建层次结构的模型如下所示:
public class IndexEntry
{
public int Score { get; set; }
//More properties that define attributes of this class
//Child objects of the hierarchy are stored in this property
public List<IndexEntry> SubEntries { get; set; }
}
如您所见,层次结构是使用 IndexEntry 类型列表构建的。
ViewModel 看起来像这样:
public class IndexEntriesViewModel
{
//TreeView ItemsSource is bound to this collection
public ObservableCollection<IndexEntry> IndexList { get; set; }
//More properties to define the ViewModel
}
如您所见,TreeView 的 ItemsSource 将绑定到 IndexEntry 类型的 ObservableCollection。我看不到任何明显的方式来访问父对象,就像现在一样。我正在考虑在模型中添加另一个属性的选项,该属性将直接指向该特定条目的父对象。这最终将允许我在层次结构中上下走动,并在需要时抓住我喜欢的东西。
所以,问题是 - 谁能想到更好的方法来实现这一点?我缺少的 TreeView 本身是否有一个属性可以提供这种能力?