简体   繁体   English

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

[英]TreeViewItem toggle doesn't work with custom style

I need to make border for all items of treeListView, up and down split line. 我需要为treeListView的所有项目(上下分割线)设置边框。 I use this code(little edited default template): 我使用以下代码(很少编辑的默认模板):

    <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>

If add "Problem Border" than my toggle buttons stop working. 如果添加“问题边框”,我的切换按钮将停止工作。 Without it all works fine. 没有它,一切正常。 How can i add a border to all items? 如何为所有商品添加边框?

When you add Border , which becomes root element of your template, you need to move VisualStateGroups from Grid to that Border (top level element of your ControlTemplate ) 添加Border ,它成为模板的根元素,您需要将VisualStateGroupsGrid移动到BorderControlTemplate顶级元素)

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
    <!-- other setters -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Border x:Name="PROBLEM BORDER" Style="{StaticResource CustomTreeViewBorder}">
                    <vs:VisualStateManager.VisualStateGroups>
                        <!-- visual states -->
                    </vs:VisualStateManager.VisualStateGroups>
                    <Grid>                  
                        <!-- Grid content -->                          
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <!-- template triggers -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM