如何在使用ControlTemplate时重现TreeViewItem中显示的文本?

时间:2012-10-03 19:02:01

标签: c# wpf wpf-controls controltemplate treeviewitem

我正在尝试修改TreeView以在其子项的上方和下方显示TreeViewItem值,例如:

foo
|bar
||baz
|biz

会变成

foo
|bar
||baz
| baz
|bar
|biz
 biz
foo

下面提供了当前控件模板的修剪表示,但不保证此代码段编译:

<ControlTemplate TargetType="{x:Type TreeViewItem}">
  <Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="19"
                          Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition/>
         <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <ToggleButton x:Name="Expander"
                    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"/>
      <TextBlock Text="Some Binding Goes Here"
                 Grid.Row="2"
                 Grid.Column="1"
                 Grid.ColumnSpan="2"/>
    </Grid>
  </ControlTemplate>

重复的条目不会有子项,它只是显示值。到目前为止,我可以使用ControlTemplate修改TreeView ContainerStyle,以在可视树中的适当位置插入TextBlock。应该使用什么数据绑定来获取适当的显示值?

1 个答案:

答案 0 :(得分:1)

为什么不再使用ContentPresenter

<ContentPresenter ContentSource="Header"
                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                  Grid.Row="2"
                  Grid.Column="1"
                  Grid.ColumnSpan="2"/>