I'm using a TreeView along with a HierarchicalDataTemplate to display a Hierarchical listing returned from a webservice. Depending on search criteria, this listing can get to be very long and several nested levels deep. It would be useful to display a "map" of sorts to the user so that they can see where in this listing they are relative to the top level. The model used to create the hierarchy looks like this:
我正在使用TreeView和HierarchicalDataTemplate来显示从Web服务返回的Hierarchical列表。根据搜索条件,此列表可能会很长并且有几个嵌套级别。向用户显示各种“地图”是有用的,这样他们就可以看到这个列表中它们相对于顶层的位置。用于创建层次结构的模型如下所示:
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; }
}
As you can see, the hierarchy is built using a List of IndexEntry types.
如您所见,层次结构是使用List of IndexEntry类型构建的。
The ViewModel looks like this:
ViewModel看起来像这样:
public class IndexEntriesViewModel
{
//TreeView ItemsSource is bound to this collection
public ObservableCollection<IndexEntry> IndexList { get; set; }
//More properties to define the ViewModel
}
As you can see, the TreeView's ItemsSource would be bound to the ObservableCollection of IndexEntry types. I don't see any obvious way to access the parent object as things are now. I am considering the option of adding another property in the model that would point directly to the parent object of that particular entry. This would ultimately allow me to walk up & down the hierarchy and grab what I like as I need it.
如您所见,TreeView的ItemsSource将绑定到IndexEntry类型的ObservableCollection。我没有看到任何明显的方式来访问父对象,就像现在的情况一样。我正在考虑在模型中添加另一个属性的选项,该属性将直接指向该特定条目的父对象。这最终将允许我在层次结构中上下移动,并根据需要抓住我喜欢的内容。
So, the question is - Can anyone think of a better way to accomplish this? Is there a property in the TreeView itself that I'm missing that will provide this ability?
所以,问题是 - 有人能想出更好的方法来实现这一目标吗? TreeView本身是否有一个我缺少的属性可以提供这种能力?
2 个解决方案
#1
There is an easy solution in the July 2009 release of the Silverlight Toolkit, the GetParentItem extension method in TreeViewExtensions.
在2009年7月发布的Silverlight Toolkit中有一个简单的解决方案,即TreeViewExtensions中的GetParentItem扩展方法。
-
Download and install the Silverlight Toolkit.
下载并安装Silverlight Toolkit。
-
Add a reference to System.Windows.Controls.Toolkit (which can be found in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Toolkit\Jul09\Bin).
添加对System.Windows.Controls.Toolkit的引用(可以在C:\ Program Files(x86)\ Microsoft SDKs \ Silverlight \ v3.0 \ Toolkit \ Jul09 \ Bin中找到)。
-
From the method you want to get the parent (I'm going to use the SelectedItemChanged event by way of example):
从您想要获取父级的方法(我将通过示例的方式使用SelectedItemChanged事件):
private void OrgTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { if (e.NewValue != null) { var parent = ((TreeView)sender).GetParentItem(e.NewValue); if (parent != null) { Status.Text = "Parent is " + parent.ToString(); } }; }
There are quite a few great extensions hiding in there that I'd encourage you to explore for setting the selected item, expanding nodes and getting the containers of items.
有很多很棒的扩展隐藏在那里,我鼓励你探索设置所选项目,扩展节点和获取项目的容器。
#2
TreeViewExtensions works for me to find the treeviewitem and parent object. View the bottom page for final.
TreeViewExtensions适用于我查找treeviewitem和父对象。查看最后一页的底部页面。
#1
There is an easy solution in the July 2009 release of the Silverlight Toolkit, the GetParentItem extension method in TreeViewExtensions.
在2009年7月发布的Silverlight Toolkit中有一个简单的解决方案,即TreeViewExtensions中的GetParentItem扩展方法。
-
Download and install the Silverlight Toolkit.
下载并安装Silverlight Toolkit。
-
Add a reference to System.Windows.Controls.Toolkit (which can be found in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Toolkit\Jul09\Bin).
添加对System.Windows.Controls.Toolkit的引用(可以在C:\ Program Files(x86)\ Microsoft SDKs \ Silverlight \ v3.0 \ Toolkit \ Jul09 \ Bin中找到)。
-
From the method you want to get the parent (I'm going to use the SelectedItemChanged event by way of example):
从您想要获取父级的方法(我将通过示例的方式使用SelectedItemChanged事件):
private void OrgTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { if (e.NewValue != null) { var parent = ((TreeView)sender).GetParentItem(e.NewValue); if (parent != null) { Status.Text = "Parent is " + parent.ToString(); } }; }
There are quite a few great extensions hiding in there that I'd encourage you to explore for setting the selected item, expanding nodes and getting the containers of items.
有很多很棒的扩展隐藏在那里,我鼓励你探索设置所选项目,扩展节点和获取项目的容器。
#2
TreeViewExtensions works for me to find the treeviewitem and parent object. View the bottom page for final.
TreeViewExtensions适用于我查找treeviewitem和父对象。查看最后一页的底部页面。