Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

I cannot pin point the issues. But as a first step you should check if the binding is working or not. you can add debugging for binding as mentioned in here

eg :

<Window 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:local="clr-namespace:DebugDataBindings"
    x:Class="DebugDataBindings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="layoutRoot">
        <Grid.Resources>
            <local:DebugConverter x:Key="debugConverter" /> 
        </Grid.Resources>
        <TextBox 
            Text="{Binding Path=Customer.FirstName, diag:PresentationTraceSources.TraceLevel=High}"   
            Height="23" HorizontalAlignment="Left" Margin="90,18,0,0" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top