1

I have a tree in WPF that has no root node, but has defined TreeViewItems for separate categories. Each of these TreeViewItems have their ItemsSource bound to the display object. The data displays fine. Now I'm trying to make the tree appear like the Windows7 Explorer trees.

I've used some of the styling from this post: WPF TreeView: How to style selected items with rounded corners like in Explorer

Anyhow here is the TreeViewItem:

<TreeViewItem ItemsSource="{Binding Path=Configuration.CognosServers}" 
                      IsExpanded="True" 
                      Visibility="{Binding ItemsSource.Count, Converter={StaticResource ResourceKey=TreeViewItemVisibility1}, RelativeSource={RelativeSource Self}}">
            <TreeViewItem.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="True"/>
                </Style>
            </TreeViewItem.ItemContainerStyle>

            <TreeViewItem.HeaderTemplate>
                <DataTemplate>
                    <Border Margin="0,5,0,0">
                        <Border.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Remove" Click="TreeItemMenu_RemoveClick" />
                                <MenuItem Header="Add Client..." Click="TreeItemMenu_AddNewClient" />
                            </ContextMenu>
                        </Border.ContextMenu>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="/WPF;component/Images/server_chart.png"
                                   Margin="0,0,5,0"/>
                            <TextBlock Text="Cognos Servers" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </TreeViewItem.HeaderTemplate>
            <TreeViewItem.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Namespaces}">
                    <Border>
                        <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                    </Border>
                    <HierarchicalDataTemplate.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource ResourceKey=TreeViewItemStyle1}">
                            <Setter Property="IsExpanded" Value="True"/>
                        </Style>
                    </HierarchicalDataTemplate.ItemContainerStyle>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Clients}">
                            <Border>
                                <TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                            </Border>
                            <HierarchicalDataTemplate.ItemContainerStyle>
                                <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource ResourceKey=TreeViewItemStyle1}">
                                    <Setter Property="IsExpanded" Value="True"/>
                                </Style>
                            </HierarchicalDataTemplate.ItemContainerStyle>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate>
                                    <Border>
                                        <TextBlock Text="{Binding DisplayName}"
                                                   ContextMenu="{StaticResource ResourceKey=ContextMenuTreeItem}" 
                                                   PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" />
                                    </Border>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeViewItem.ItemTemplate>
        </TreeViewItem>

The style I'm apply to the TreeView appears like this:

<TreeView x:Name="TreeViewLoadedItems" 
              Grid.Row="1"
              VerticalAlignment="Stretch" 
              ItemContainerStyle="{DynamicResource TreeViewItemStyle1}" 
              MouseDoubleClick="TreeViewItem_MouseDoubleClick"
              SelectedItemChanged="TreeViewLoadedItems_SelectedItemChanged" >

Finally the style I'm applying is this:

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type TreeViewItem}">
                <!-- Style for the selected item -->
                <Setter Property="BorderThickness" Value="1"/>
                <Style.Triggers>
                    <!-- Selected and has focus -->
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderBrush" Value="#7DA2CE"/>
                    </Trigger>
                    <!-- Mouse over -->
                    <!--<Trigger Property="helpers:TreeView_IsMouseDirectlyOverItem.IsMouseDirectlyOverItem" Value="True">
                                                <Setter Property="Background">
                                                    <Setter.Value>
                                                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                            <GradientStop Color="#FFFAFBFD" Offset="0"/>
                                                            <GradientStop Color="#FFEBF3FD" Offset="1"/>
                                                        </LinearGradientBrush>
                                                    </Setter.Value>
                                                </Setter>
                                                <Setter Property="BorderBrush" Value="#B8D6FB"/>
                                            </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="#D9D9D9"/>
                    </MultiTrigger>
                </Style.Triggers>
                <Style.Resources>
                    <Style TargetType="Border">
                        <Setter Property="CornerRadius" Value="2"/>
                    </Style>
                </Style.Resources>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

No Matter what I've done I can't seem to get each item to look the same when selected. Only the leaf nodes seem to be picking up the style. I always have trouble when there are HierarchialDataTemplates and I need to style things. What gives?!

Community
  • 1
  • 1
Mike G
  • 1,956
  • 1
  • 30
  • 62

1 Answers1

4

In your style you set the ItemContainerStyle, why? That way the immediate TreeViewItem the style is applied to will not have the style, maybe that somehow even propagates down to the leaf; you should not need to set that property. Just place a style for TreeViewItems in the Resources of the TreeView and it will automatially apply to each item, move everything in the ItemContainerStyle up to the actual main style.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 1
    This helps some, but now I'm loosing the style on the children of that TreeViewItem. The TreeViewItem is getting the style which I haven't been able to do. Is it because I'm placing a style in the HierarchicalDataTemplate.ItemContainerStyle which now doesn't have a style for the BasedOn property? – Mike G Nov 02 '11 at 21:11
  • 2
    Yep. I forgot I could do BasedOn="{StaticResource {x:Type TreeViewItem}}. That way I can auto expand the nodes and not loose the style. It was exactly as you said. Thanks! – Mike G Nov 02 '11 at 21:15