【问题标题】:How can I make the background color of a node in a TreeView goes all the way out to the right edge?如何使 TreeView 中节点的背景颜色一直到右边缘?
【发布时间】:2010-12-20 08:57:47
【问题描述】:

如何使 TreeView 中节点的背景颜色一直到右边缘?

背景

我有一个 TreeView,其中所有有子节点的节点都是灰色的。叶节点有一个带有描述性文本的图标。

【问题讨论】:

    标签: c# wpf treeview wpf-controls


    【解决方案1】:

    如果我理解正确的问题,您正在寻找拉伸树视图项目。

    如果是这种情况,请检查Here

    【讨论】:

      【解决方案2】:

      这很容易解决。不幸的是,它需要相当多的代码,因为您必须重新模板化 TreeViewItem。 TreeViewItem 的模板如下所示

      <ControlTemplate TargetType="{x:Type TreeViewItem}">
          <Grid>
              <Grid.ColumnDefinitions>
                  <!-- The Expand/Collapse ToggleButton -->
                  <ColumnDefinition MinWidth="19" Width="Auto"/>
                  <!-- The TreeViewItem -->
                  <ColumnDefinition Width="Auto"/>
                  <!-- The Children -->
                  <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition/>
              </Grid.RowDefinitions>
              <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
              <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                  <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
              </Border>
              <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
          </Grid>
      

      如您所见,TreeViewItem 本身位于 Column 1 中,其 Width 设置为 Auto,而子项 (ItemsPresenter) 位于 Grid.Column 1 和 2 中,Column 2 的 Width 设置为 *。为了让 TreeViewItem 本身获得与其子项相同的宽度,我们可以删除中间列(列 1),然后对两者都使用 Grid.Column="1"。

      这是具有此“水平拉伸”修复的 TreeViewItem 的默认模板。您必须添加对 PresentationFramework.Aero 的引用

      <Style x:Key="TreeViewItemFocusVisual">
          <Setter Property="Control.Template">
              <Setter.Value>
                  <ControlTemplate>
                      <Rectangle/>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>
      <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
      <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
          <Setter Property="Focusable" Value="False"/>
          <Setter Property="Width" Value="16"/>
          <Setter Property="Height" Value="16"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type ToggleButton}">
                      <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16">
                          <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="Transparent" Stroke="#FF989898">
                              <Path.RenderTransform>
                                  <RotateTransform Angle="135" CenterY="3" CenterX="3"/>
                              </Path.RenderTransform>
                          </Path>
                      </Border>
                      <ControlTemplate.Triggers>
                          <Trigger Property="IsMouseOver" Value="True">
                              <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
                              <Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
                          </Trigger>
                          <Trigger Property="IsChecked" Value="True">
                              <Setter Property="RenderTransform" TargetName="ExpandPath">
                                  <Setter.Value>
                                      <RotateTransform Angle="180" CenterY="3" CenterX="3"/>
                                  </Setter.Value>
                              </Setter>
                              <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                              <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                          </Trigger>
                      </ControlTemplate.Triggers>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>
      <Style x:Key="StretchTreeViewItemStyle"
             TargetType="{x:Type TreeViewItem}"
             xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
          <Setter Property="Background" Value="Transparent"/>
          <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
          <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
          <Setter Property="Padding" Value="1,0,0,0"/>
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
          <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type TreeViewItem}">
                      <Grid>
                          <Grid.ColumnDefinitions>
                              <ColumnDefinition MinWidth="19" Width="Auto"/>
                              <ColumnDefinition Width="*"/>
                          </Grid.ColumnDefinitions>
                          <Grid.RowDefinitions>
                              <RowDefinition Height="Auto"/>
                              <RowDefinition/>
                          </Grid.RowDefinitions>
                          <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                          <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                              <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                          </Border>
                          <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1"/>
                      </Grid>
                      <ControlTemplate.Triggers>
                          <Trigger Property="IsExpanded" Value="false">
                              <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                          </Trigger>
                          <Trigger Property="HasItems" Value="false">
                              <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                          </Trigger>
                          <Trigger Property="IsSelected" Value="true">
                              <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                          </Trigger>
                          <MultiTrigger>
                              <MultiTrigger.Conditions>
                                  <Condition Property="IsSelected" Value="true"/>
                                  <Condition Property="IsSelectionActive" Value="false"/>
                              </MultiTrigger.Conditions>
                              <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                          </MultiTrigger>
                          <Trigger Property="IsEnabled" Value="false">
                              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                          </Trigger>
                      </ControlTemplate.Triggers>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
          <Style.Triggers>
              <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                  <Setter Property="ItemsPanel">
                      <Setter.Value>
                          <ItemsPanelTemplate>
                              <VirtualizingStackPanel/>
                          </ItemsPanelTemplate>
                      </Setter.Value>
                  </Setter>
              </Trigger>
          </Style.Triggers>
      </Style>
      

      然后你就可以这样使用了

      <TreeView ...>
          <TreeView.ItemContainerStyle>
              <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StretchTreeViewItemStyle}">
                  <!-- Add your own setters if applicable -->
              </Style>
          </TreeView.ItemContainerStyle>
      </TreeView>
      

      【讨论】:

        猜你喜欢
        • 2012-10-04
        • 1970-01-01
        • 2015-10-23
        • 2014-09-05
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多