1

我想构建一个具有以下功能的 TreeView:

  1. 第一级的扩展按钮应该不在左侧,而是在右侧
  2. 所有子级别都应该在左侧有扩展按钮 - 就像默认的 TreeViewItem
  3. 展开/折叠应该像展开器控件一样进行动画处理

我在搞乱 DataTemplate 和 HierarchicalDataTemplate,但 Expander-Button 似乎不是模板的一部分,它总是添加到模板中并且总是在左侧。如何根据项目级别放置 Expander-Button?

4

1 回答 1

1

You have to customize the TreeViewItem ControlTemplate, by changing the ToggleButton column. Then you can just change the controltemplate using a DataTrigger inside a style.

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
  <!-- snip -->
  <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/>
      </Grid.RowDefinitions>
      <ToggleButton Grid.Column="1" x:Name="Expander"
              Style="{StaticResource ExpandCollapseToggleStyle}"
              IsChecked="{Binding Path=IsExpanded,
                          RelativeSource={RelativeSource TemplatedParent}}"
              ClickMode="Press"/>
      <Border Name="Bd"
          Grid.Column="0"
          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>
    <ControlTemplate.Triggers>
      <!-- snip -->
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Style>
于 2013-02-15T07:40:20.597 回答