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

时间:2015-07-17 08:24:12

标签: wpf xaml treeview styles selecteditem

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

 <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:值已更改。

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

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

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

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

    }

0 个答案:

没有答案