C# TreeViewItem切换不适用于自定义样式

C# TreeViewItem切换不适用于自定义样式,c#,wpf,treeview,C#,Wpf,Treeview,我需要为treeListView的所有项目制作边框,上下分割线。 我使用此codelittle编辑的默认模板: <Style x:Key="CustomTreeViewBorder" TargetType="{x:Type Border}"> <Setter Property="BorderThickness" Value="0 1 0 1" /> <Setter Property="BorderBrush" Value="{StaticRe

我需要为treeListView的所有项目制作边框,上下分割线。 我使用此codelittle编辑的默认模板:

    <Style x:Key="CustomTreeViewBorder" TargetType="{x:Type Border}">
    <Setter Property="BorderThickness" Value="0 1 0 1" />
    <Setter Property="BorderBrush" Value="{StaticResource LogTreeViewDataBorderBrush}" />
</Style>
<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Padding" Value="1,0,0,0" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <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}">
                <Border x:Name="PROBLEM BORDER" Style="{StaticResource CustomTreeViewBorder}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" MinWidth="19" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <vs:VisualStateManager.VisualStateGroups>
                            <vs:VisualStateGroup x:Name="SelectionStates">
                                <vs:VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="ExpanderBorder" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Panel.Background).                     (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vs:VisualState>
                                <vs:VisualState x:Name="Unselected" />
                                <vs:VisualState x:Name="SelectedInactive">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="ExpanderBorder" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedUnfocusedColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Panel.Background).                     (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="{StaticResource SelectedUnfocusedColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vs:VisualState>
                            </vs:VisualStateGroup>
                            <vs:VisualStateGroup x:Name="ExpansionStates">
                                <vs:VisualState x:Name="Expanded">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="(UIElement.Visibility)">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vs:VisualState>
                                <vs:VisualState x:Name="Collapsed" />
                            </vs:VisualStateGroup>
                        </vs:VisualStateManager.VisualStateGroups>
                        <Border x:Name="ExpanderBorder">
                            <Border.Background>
                                <SolidColorBrush Color="Transparent" />
                            </Border.Background>
                            <ToggleButton x:Name="Expander"
                                      ClickMode="Press"
                                      IsChecked="{Binding IsExpanded,
                                                          RelativeSource={RelativeSource TemplatedParent}}"
                                      Style="{StaticResource ExpandCollapseToggleStyle}" />
                        </Border>
                        <Border x:Name="Bd"
                            Grid.Row="0"
                            Grid.Column="1"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                            <ContentPresenter x:Name="PART_Header"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          ContentSource="Header" />
                        </Border>
                        <ItemsPresenter x:Name="ItemsHost"
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    Visibility="Collapsed" /></Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="Expander" Property="Visibility" Value="Hidden" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false" />
                            <Condition Property="Width" Value="Auto" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinWidth" Value="75" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false" />
                            <Condition Property="Height" Value="Auto" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinHeight" Value="19" />
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
如果添加问题边框,我的切换按钮将停止工作。没有它,一切都很好。如何向所有项目添加边框?

添加边框时,它将成为模板的根元素,您需要将VisualStateGroups从网格移动到ControlTemplate的该边框顶级元素