开发者

Column Header for a WPF TreeView

开发者 https://www.devze.com 2022-12-31 00:25 出处:网络
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

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.


You can try to reuse your SharedSizeGroup definitions. For example put another grid above your TreeView for your header and place them inside a stackpanel like:

        <StackPanel Grid.IsSharedSizeScope="True" Orientation="Vertical">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="NameColumn" />
                    <ColumnDefinition SharedSizeGroup="GenreColumn" />
                    <ColumnDefinition SharedSizeGroup="BornColumn" />
                    <ColumnDefinition SharedSizeGroup="DiedColumn" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Name"/>
                <TextBlock Grid.Column="1" Text="Genre"/>
                <TextBlock Grid.Column="2" Text="Born"/>
                <TextBlock Grid.Column="3" Text="Dies"/>
            </Grid>
            <TreeView>
               ...
            </TreeView>
        </StackPanel>

The Grid.IsSharedSizeScope=True will reuse your SharedSizeGroups for your header and tree.

You can also style your Header separately.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号