如何在 TreeView 中为所选项目设置颜色

How to set color to selected item at TreeView

我尝试将红色设置为 TreeView 上的 selected 项目:

 <TreeView x:Name="tvVisual" Grid.Row="0" Grid.RowSpan="2"
                  VirtualizingStackPanel.IsVirtualizing="True" 
ItemsSource="{Binding Childrens,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"      
                  VirtualizingStackPanel.VirtualizationMode="Recycling"
                  >
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate 
ItemsSource="{Binding Childs,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                    <TextBlock Text="{Binding Path=Value.Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>


            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}" >
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />                           
                    <Setter Property="FontWeight" Value="Normal" />                            

                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                            <Setter Property="Background" Value="Bisque"/>
                        </Trigger>

                        <!-- Selected but does not have the focus -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True"/>
                                <Condition Property="IsSelectionActive" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="Red"/>
                        </MultiTrigger>
                    </Style.Triggers>                        
                </Style>
            </TreeView.ItemContainerStyle>


        </TreeView>

我从 this Sheridan answer 中提取了部分代码,但它对我不起作用。 如果我将 IsSelected 属性 更改为 IsExpanded - 它有效。 在我的 getter\setter:值已更改。

可能会有所帮助:我select并按代码展开所有子节点。

那么,如何让IsSelected项设置新的颜色呢? 谢谢!

P.S。这很奇怪 - 但在 getter\setter PropertyChanged 为空...

 private bool _isSelected;      
    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected"); //PropertyChanged is null.
        }

    }
      <TreeView x:Name="tvVisual" BorderBrush="{DynamicResource scbTreeCategories}" BorderThickness="5"
                  VirtualizingStackPanel.IsVirtualizing="False"
                  ItemsSource="{Binding Childrens,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  ScrollViewer.CanContentScroll="False"
                  TreeViewItem.Selected="tvVisual_Selected"                                                                   
                  >

              <TreeView.ItemTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding Childs,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                    <TextBlock Text="{Binding Path=Name}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                        <!--<Setter Property="Background" Value="#4682B4" />-->
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Setter Property="Background" Value="Blue"/>
                                        <Setter Property="Foreground" Value="White"/>

                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IsTargetItem}" Value="True">
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Setter Property="Background" Value="Blue"/>
                                        <Setter Property="Foreground" Value="White"/>
                                   </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

            <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}" >
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    </Style>
                </TreeView.ItemContainerStyle>


        </TreeView>