wpf 使用 Treeview HierarchicalDataTemplate.Triggers 更改文件夹图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18951473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:42:02  来源:igfitidea点击:

Using the Treeview HierarchicalDataTemplate.Triggers to change folder icon

wpfxamltreeviewhierarchicaldatatemplate

提问by Hadi Mostafavi

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:

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

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

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

回答by Nitin

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

如果您绑定到该TreeViewItem IsExpanded属性,则必须更新您的绑定,例如:

<DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True">
   <Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
</DataTrigger>

回答by Manish Kumar

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>