Column Header for a WPF TreeView
- by nareshbhatia
I am using the WPF TreeView to display some hierarchical information. Each item in the TreeView consists of several attributes, so I am using a Grid within my HierarchicalDataTemplate to display these attributes:
<HierarchicalDataTemplate x:Key="ArtistTemplate"
ItemsSource="{Binding XPath=Title}"
ItemTemplate="{StaticResource TitleTemplate}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="NameColumn" />
<ColumnDefinition SharedSizeGroup="GenreColumn" />
<ColumnDefinition SharedSizeGroup="BornColumn" />
<ColumnDefinition SharedSizeGroup="DiedColumn" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding XPath=@Name}" />
<TextBlock Grid.Column="1" Text="{Binding XPath=@Genre}" />
<TextBlock Grid.Column="2" Text="{Binding XPath=@Born}" />
<TextBlock Grid.Column="3" Text="{Binding XPath=@Died}" />
</Grid>
</HierarchicalDataTemplate>
This displays as a nice TreeView with 4 columns - so far so good! The only additional thing I need is a header above the TreeView that displays column names. The header column widths should be synchronized with TreeViewItems and also the header styles should be customizable. What's the easiest way to do this?
P.S. I found two solutions that came close:
1) A TreeListView here, but this requires me to implement a custom interface (ITreeModel) to my model. Also the approach in this solution is to start with a ListView and to implement a RowExpander manually. In my case, the TreeView is sufficiently close to what I need, so I am hoping that putting a header on it should be very simple.
2) A TreeListView here. This one indeed starts with a TreeView, but I can't figure out how to customize the header. I suspect that I have to customize the GridViewHeaderRowPresenter in the generic.xaml, but this element does not seem to have its own ControlTemplate.