【问题标题】:Different WPF Treeview icons depending on the type of node不同的 WPF Treeview 图标取决于节点的类型
【发布时间】:2011-11-25 07:42:46
【问题描述】:

我需要将图像添加到 WPF 树视图节点,我已经查看了这个示例 How do I add icons next to the nodes in a WPF TreeView?,它工作正常,除了所有节点都具有相同的图像。我想要树视图中没有的所有节点任何孩子要么没有形象,要么有不同的形象。

这是我设置图像的 XAML:

   <HierarchicalDataTemplate x:Key="NodeTemplate">
        <StackPanel Orientation="Horizontal" Margin="2">
            <Image Source="test.png"  Width="16" Height="16" SnapsToDevicePixels="True"/>
        <TextBlock x:Name="tb"/>
        </StackPanel>
        <HierarchicalDataTemplate.ItemsSource>
            <Binding>
                <Binding.XPath>child::node()</Binding.XPath>
            </Binding>
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>

下面是输出的屏幕截图

有人可以建议我如何实现这一点,可能的解决方案可以是更改 XAML 或通过 C# 以编程方式。

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    这是我用来解决几乎相同问题的一些代码。 (我这个设计的数据是XML数据,所以XPath="@name"表示节点属性名的值,而Name表示元素类型。)

    <Window x:Class="NodeExplorer2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:NodeExplorer2">
        <Window.Resources>
            <my:PathConverter x:Key="iconConverter"/>
    
            <HierarchicalDataTemplate x:Key="XmlTreeTemplate">
                <HierarchicalDataTemplate.ItemsSource>
                    <Binding XPath="child::node()" />
                </HierarchicalDataTemplate.ItemsSource>
    
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="icon" SnapsToDevicePixels="True" Stretch="None" Margin="0,0,3,0" />
                    <TextBlock Text={Binding XPath="@name"/>
                </StackPanel>
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                        <Setter TargetName="icon" Property="Source">
                            <Setter.Value>
                                <Binding Path="Name" Converter="{StaticResource iconConverter}">
                                    <Binding.FallbackValue>
                                        <ImageSource>
                                            Data/Icons/unknown.png
                                        </ImageSource>
                                    </Binding.FallbackValue>
                                </Binding>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>
            </HierarchicalDataTemplate>
    

    转换器:

    public class PathConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Console.WriteLine("Value:" + value);
                return "Data/Icons/" + value + ".png";
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }
    

    【讨论】:

    • 嗨 AkselK,我是 WPF 的新手,您能否解释一下 是什么以及 xaml 必须放在哪里?我把它放在 标签,但我得到以下编译时错误 - “我的”是一个未声明的前缀
    • 啊,我明白了。 my: 是一个命名空间,在 xaml 顶部的 标记中声明。就我而言,我已将 my: 声明为 xmlns:my="clr-namespace:NodeExplorer2" 在您的情况下,它将是 xmlns:my="clr-namespace:Your_project_name"。以上所有代码都在 中声明,位于您的第一个实际组件(可能是)上方。 my:PathConverter 简单来说就是我声明了一个 PathConverter 类型的对象,而 x:Key=iconConverter 意味着我可以通过 {StaticResource iconConverter} 引用它。
    • 酷,非常感谢您的解释!我只需要添加一个小方法来检查特定节点是否有任何子节点,如果没有,那么我将节点图像更改为子图标。 ..将此标记为已回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多