问题描述
我当前正在编写WPF TreeListView.我想知道几件事.
I am currently writing a WPF TreeListView.I was wondering a couple of things.
层次数据结构如何工作,并且子级可以显示其他属性,而不是与父级相同的属性?目前,我正在尝试创建带有列的treelistview
How does the Hierarchical Data Structure work and can the children display other properties instead of the same property as the parent?Currently I am trying to make a treelistview with the columns
客户/事务/工时
如果我添加一个客户端,并且有相同的问题,那么家长会将小时数改为总时数,改为孩子加起来的总时数.
If i add a client and it has the same matter thenthe parent will change it's hours to the total hours to the total amount of hours added up by the children.
这是一个例子
我添加
约翰/书写纸/1小时
约翰/书写纸/2小时
约翰/写作纸/.5小时
John / Writing Paper / 1 hour
John / Writing Paper / 2 hour
John / Writing Paper / .5 Hours
我的树形列表视图将显示
My tree list view will show
约翰/书写纸/3.5小时<这是父母
-约翰/书写纸/1小时
-约翰/书写纸/2小时
-约翰/写作纸/.5小时<这些是孩子
John / Writing Paper /3.5 Hours < this is the parent
-John / Writing Paper / 1 hour
-John / Writing Paper / 2 hour
-John / Writing Paper / .5 Hours < these are the children
相反,我希望它显示
约翰/写论文/3.5小时
-上午12:00-下午1:00/写了简介
-2:00 pm-4:00 pm/写下尸体
-3:00 pm-3:30 pm/写下结论
John / WRiting Paper/ 3.5 Hours
- 12:00 am - 1:00 pm / wrote the introduction
- 2:00 pm - 4:00 pm / wrote the body
- 3:00 pm - 3:30 pm / wrote the conclusion
我正在使用两个可观察的集合.一个是父母,另一个是孩子.
I am using two observable collections. One which is the parent and one which is the Child.
我的问题基本上是.我可以更改分层数据结构以显示不同的属性吗?不同的信息.我不想重复显示相同的客户事务时间.相反,我想为孩子展示不同的属性.因为家长将显示信息属于谁.顺便说一句,我正在尝试针对XAML和C#
My question basically is. Can I change the Hierarchical Data Structure to display different properties? Different information. I don't want to be repetitive on showing the same Client Matter Hours.Instead i would like to show different properties for the child. Since the Parent will be showing who the information belongs to.Btw, I'm trying to do this for XAML and C#
先谢谢了!!
-凯文
Thanks in Advance!!
-Kevin
推荐答案
如果您的父母和孩子是不同的对象类型,则有一个非常简单的答案:只需在ResourceDictionary中使用多个HierarchicalDataTemplates:
If your parent and child are different object types, there is a very easy answer: Just use multiple HierarchicalDataTemplates in a ResourceDictionary:
<TreeView ItemsSource="{Binding Parents}">
<TreeView.ResourceDictionary>
<HierarchicalDataTemplate
TargetType="{x:Type my:ParentType}"
ItemsSource="{Binding Children}">
... parent content ...
</HierarchicalDataTemplate>
<HierarchicalDataTemplate
TargetType="{x:Type my:ChildType}"
ItemsSource="{Binding Children}">
... child content ...
</HierarchicalDataTemplate>
</TreeView.ResourceDictionary>
</TreeView>
此技术并非在所有情况下都适用,但是一旦启用,它就会非常强大且富有表现力.
This technique does not work in all scenarios, but when it does it is very powerful and expressive.
如果父级"和子级"是相同类型但参数不同,则对此的另一种变化是创建一个ItemTemplateSelector
,该ItemTemplateSelector
调用LoadResource()
来根据数据值加载适当的已命名的HierarchicalDataTemplate
.
Another variation on this if the Parent and Child are the same type but with a different parameter is to create an ItemTemplateSelector
that calls LoadResource()
to load the appropriate named HierarchicalDataTemplate
depending on the data values.
这篇关于分层数据结构WPF TreeListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!