How to make a footer section on a hierarchical data template
I am trying to implement a treeView that has a footer section at the bottom of each hierarchy so a user can easily add items to the bottom.
Something like
<TreeView>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="local:MyNestedType"
ItemsSource="{Binding Items}">
<StackP开发者_运维问答anel>
<Label Content="{Binding NodeName}" />
<!--Nested Items here-->
<Button Content="Add New Item" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
How can I achieve this.
The answer isn't pretty, but it is doable.
You'll have to override the ItemContainerStyle
of the TreeView
, and define a control template.
http://msdn.microsoft.com/en-us/library/ms788727.aspx
In that example, you'll see that the TreeViewItem
control template has some XAML like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<Border Name="Bd"
Grid.Column="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter x:Name="PART_Header"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"/>
</Grid>
That XAML defines how child items are shown relative to the parent item - under the ItemsPresenter
you could insert your footer.
精彩评论