Hierarchical Data Structure WPF TreeListView
I am currently writing a WPF TreeListView. I was wondering a couple of things.
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
Client / Matter / Hours
If i add a client and it has the same matter then the parent will change it's hours to the total hours to the total amount of hours added up by the children.
Here's an example
I add
John / Writing Paper / 1 hour
John / Writing Paper / 2 hourJohn / Writing Paper / .5 Hours
My tree list view will show
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
Instead i would like it to show
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.
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 Clie开发者_JAVA技巧nt 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!!
-KevinYou can set DataTrigger for the HierarchicalDataTemplate and have different property binding. Just an example below. Please check this earlier answer in this thread just in case you need more ideas.
<HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding People}" >
<Grid>
<TextBlock x:Name="fName" Text="{Binding FirstName}"/>
<TextBlock x:Name="lName" Text="{Binding LastName}" Visibility="Collapsed"/>
</Grid>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding State}" Value="A">
<Setter TargetName="fName" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="lName" Property="Visibility" Value="Visible"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
Jobi Joy
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.
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.
精彩评论