带有图标和文本的 WPF TreeView

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

WPF TreeView with icons and text

c#wpf

提问by Tsukasa

Working on learning WPF by converting my win forms apps. Currently having a hard time figuring out how to add TreeViewItems to a TreeView that contains am image before text all on the same line. My images are png files that are listed as a resource.

通过转换我的 win 表单应用程序来学习 WPF。目前很难弄清楚如何将 TreeViewItems 添加到在同一行上的文本之前包含图像的 TreeView。我的图像是列为资源的 png 文件。

I want to be able to specify the image and text of each item. Everything I have been seeing are setting default images.

我希望能够指定每个项目的图像和文本。我所看到的一切都是设置默认图像。

Would really appreciate the help.

非常感谢您的帮助。

Thanks

谢谢

回答by Iain

Try something like this:

尝试这样的事情:

    <TreeView ItemsSource="{Binding SomeData}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Icon}" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

See this for how to bind the image in more detail: Binding image source through property in wpf

有关如何更详细地绑定图像,请参阅此内容: 通过 wpf 中的属性绑定图像源