Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Treeview HierarchicalDataTemplate.Triggers to change folder icon

I'm pretty fresh to WPF and this is the closest I have come to achieving what I set out to do after reviewing many of the previously asked questions posted here. The XAML code:

<TreeView x:Name="folderView" Grid.Column="0" Grid.Row="1" BorderThickness="0">
    <TreeViewItem Header="Folders" ItemsSource="{Binding SubFolders, Source={StaticResource RootFolderDataProvider}}" Margin="5"/>

    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type my:FolderView}" ItemsSource="{Binding SubFolders}">
            <StackPanel Orientation="Horizontal" Name="myPanel">
                <Image x:Name="img" Width="16" Height="16" Source="Images/FolderClosed.png" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>

            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsExpanded}" Value="True">
                    <Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>    
    </TreeView.Resources>
</TreeView>

This displays the FolderClosed image on all my subfolders except the very top root folder. The HierachicalDataTemplate trigger also fails to fire when expanded. Any help would be appreciated.

like image 838
Hadi Mostafavi Avatar asked Sep 23 '13 03:09

Hadi Mostafavi


1 Answers

If you are binding to the TreeViewItem IsExpanded property, then you will have to update your binding like:

<DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True">
   <Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
</DataTrigger>
like image 97
Nitin Avatar answered Oct 16 '22 00:10

Nitin