treeview实现多级子项,并且让每一级子项都右对齐

<TreeView 
   <TreeView.ItemContainerStyle>
      <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" AllowDrop="False" 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.Resources>
               <!--SelectedItem with focus-->
               <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCCCCC" Opacity="1"/>
           </Style.Resources>

           <Style.Triggers>
               <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                   <Setter Property="ItemsPanel">
                       <Setter.Value>
                           <ItemsPanelTemplate>
                               <VirtualizingStackPanel/>
                           </ItemsPanelTemplate>
                       </Setter.Value>
                   </Setter>
               </Trigger>
           </Style.Triggers>
<TreeView.ItemTemplate >
    <HierarchicalDataTemplate  ItemsSource="{Binding DevicesChild}">
//多级子项

原文:https://www.cnblogs.com/mqxs/p/15180699.html

posted @ 2025-03-20 15:07  哥白尼·  阅读(28)  评论(0)    收藏  举报