WPF Treeview: How to display rounded border around the selected item?

Posted by chikak on Stack Overflow See other posts from Stack Overflow or by chikak
Published on 2010-12-26T10:50:27Z Indexed on 2010/12/26 10:53 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

I am trying to set the rounded border around the selected item of the treeview (like the one in the windows explorer on Vista). The problem is that the trigger in the following code doesn't seem to be working. It looks like the IsSelected property is always false.

 <TreeView x:Name="m_treeView" BorderThickness="0" d:LayoutOverrides="Width, Height">
            <TreeView.Resources>
                <Style TargetType="TreeViewItem">
                    <Style.Resources>
                        <Brush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</Brush>
                    </Style.Resources>
                </Style>
            </TreeView.Resources>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type local:DirectoryPresentationBase}" ItemsSource="{Binding Children}">
                    <TreeViewItem>
                        <TreeViewItem.Header>
                            <Border  Name="SelectedBorder"  CornerRadius="3" Background="#EFF8FD" BorderBrush="#99DEFD"
                                     BorderThickness="1" >
                                <StackPanel Orientation="Horizontal">
                                    <interactivity:Interaction.Behaviors>
                                        <local:TreeViewExpandBehavior AssociatedTreeView="{Binding ElementName=m_treeView}"/>
                                    </interactivity:Interaction.Behaviors>
                                    <Image x:Name="img" Width="24" Height="16" Stretch="None" Source="{Binding SmallIcon}"/>
                                    <TextBlock Text="{Binding Name}" Margin="5,0" />
                                </StackPanel>
                            </Border>
                        </TreeViewItem.Header>
                    </TreeViewItem>
                    <HierarchicalDataTemplate.Triggers>
                        <Trigger Property="TreeViewItem.IsSelected" Value="True">
                            <Setter Property="Background" TargetName="SelectedBorder"  Value="Red"/>
                            <Setter Property="BorderBrush" TargetName="SelectedBorder"  Value="Green"/>
                        </Trigger>
                        <Trigger Property="TreeViewItem.IsSelected" Value="False">
                            <Setter Property="Background" TargetName="SelectedBorder"  Value="Transparent"/>
                            <Setter Property="BorderBrush" TargetName="SelectedBorder"  Value="Transparent"/>
                        </Trigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf