.net 如何在 WPF TreeView 中的节点旁边添加图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/250597/
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-03 10:28:10  来源:igfitidea点击:

How do I add icons next to the nodes in a WPF TreeView?

.netwpftreeview

提问by Rob Sobers

I have a WPF TreeView with just 1 level of items. The TreeView is data bound to an ObservableCollection of strings. How can I ensure that the same icon appears to the left of each node in the TreeView?

我有一个只有 1 个级别的项目的 WPF TreeView。TreeView 是绑定到字符串 ObservableCollection 的数据。如何确保相同的图标出现在 TreeView 中每个节点的左侧?

回答by James Osborn

I think the best approach is to set a Style on the TreeView that will change the Template of the TreeViewItems to have the Image that you want.

我认为最好的方法是在 TreeView 上设置一个样式,它将更改 TreeViewItems 的模板以拥有您想要的图像。

The Template will probably need to be a StackPanel with an Image and a label control, you bind the image to your icon, and the label text to the strings from the Observable collection.

模板可能需要是一个带有图像和标签控件的 StackPanel,您将图像绑定到您的图标,并将标签文本绑定到 Observable 集合中的字符串。

I've copied the relevant code snippet from a Code Project article, which covers this in more detail, but I think the below is all you'll need (This code goes in the TreeView.Resources element).

我已经从Code Project 文章中复制了相关的代码片段,其中更详细地介绍了这一点,但我认为以下就是您所需要的(此代码位于 TreeView.Resources 元素中)。

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Name="img"
                           Width="20"
                           Height="20"
                           Stretch="Fill"
                           Source="image.png"/>
                    <TextBlock Text="{Binding}" Margin="5,0" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

回答by EisenbergEffect

I think one of the best articles that will help you to understand the TreeView is this one http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx. In general, this describes a good set of patterns that can make a lot of scenarios in WPF/SL much easier.

我认为可以帮助您理解 TreeView 的最佳文章之一是http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx。总的来说,这描述了一组很好的模式,可以使 WPF/SL 中的许多场景变得更容易。

回答by Zack Peterson

I used James Osborn's StackPanel techniquein this way...

我以这种方式使用了James OsbornStackPanel 技术......

XAML:

XAML:

<TreeView Name="TreeViewThings" ItemsSource="{Binding}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Thing}"
                                  ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal" Margin="2">
                <Image Source="Thing.png"
                       Width="16"
                       Height="16"
                       SnapsToDevicePixels="True"/>
                <TextBlock Text="{Binding Path=Name}" Margin="5,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>