I try to set Red color to selected item on 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>
I took some part of code from this Sheridan answer, but it not works for me. If i change IsSelected property to IsExpanded - it works. At my getter\setter: value changed.
May be it can help: i select and expand all subnodes by code.
So, how to make IsSelected item set new color? Thank you!
P.S. and it is strange- but at getter\setter PropertyChanged is null...
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged("IsSelected"); //PropertyChanged is null.
}
}