0

The following code is treeview:

   <TreeView BorderThickness="1,1,1,1" BorderBrush="#ffcccccc"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
              ItemTemplate="{StaticResource itemTypeTreeViewTemplate}"
              ItemsSource="{Binding ItemTypes}"
              ItemContainerStyle="{StaticResource treeViewItemStyle}"/>

And i set the itemTemplate to set the binding of items. Then set the ItemContainerStyle to change IsSelected style:

  <Style x:Key="treeViewItemStyle" TargetType="TreeViewItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightBlue" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <HierarchicalDataTemplate x:Key="itemTypeTreeViewTemplate" 
                              ItemsSource="{Binding Child}">
                <DockPanel Margin="0,5,0,5">
                    <Button VerticalAlignment="Center"
                        x:Name="btn1" Width="25"
                        Visibility="{Binding Path=IsMouseOver,RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter }}"
                        Content="U" DockPanel.Dock="Right" cal:Message.Attach="[Event Click] = [Action EditItemType($dataContext)]" 
                        Style="{StaticResource ButtonStyle1}" />
                    <TextBlock Margin="0,2,0,2" VerticalAlignment="Center" Text="{Binding ItemTypeName}" Foreground="#FF2e8bcc" FontSize="10" FontFamily="微软雅黑" />
                </DockPanel>    
    </HierarchicalDataTemplate>

But the isSelected style is not work. Anybody find the key??

LanPst
  • 11
  • 2
  • Use a behaviour like this post? [here](http://stackoverflow.com/questions/11065995/binding-selecteditem-in-a-hierarchicaldatatemplate-applied-wpf-treeview) – Esperento57 Jul 17 '16 at 08:46

1 Answers1

0

Instead of using a style trigger, you should override the colour key for the highlighted state. Here's the code:

<Style x:Key="treeViewItemStyle" TargetType="TreeViewItem">
    <!--<Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="LightBlue" />
        </Trigger>
    </Style.Triggers>--><!--Remove Style.Trigger block-->
    <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                   Color="LightBlue"/>
        </Style.Resources>
</Style>

Hope this helps. The other keys for this control are:

  • HighlightBrushKey - Focussed background. (Is focussed when selected)
  • HighlightTextBrushKey - Focussed foreground.
  • InactiveSelectionHighlightBrushKey - Normal background.
  • InactiveSelectionHighlightTextBrushKey - Normal foreground.
Fred Truter
  • 667
  • 4
  • 10
Lew
  • 1,248
  • 8
  • 13