5

我对 WPF 非常陌生,这是我在查看了此处发布的许多先前提出的问题后最接近实现我的目标的方式。XAML 代码:

<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>

这将在我的所有子文件夹上显示 FolderClosed 图像,除了最顶层的根文件夹。触发器在HierachicalDataTemplate展开时也无法触发。任何帮助,将不胜感激。

4

2 回答 2

7

如果您要绑定到该TreeViewItem IsExpanded属性,那么您将必须更新您的绑定,例如:

<DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True">
   <Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
</DataTrigger>
于 2013-09-23T04:10:18.667 回答
-2

我无法确定问题所在。但作为第一步,您应该检查绑定是否有效。您可以按照此处所述添加绑定调试

例如:

<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>
于 2013-09-23T04:04:11.203 回答