如何在WPF TreeView中添加和显示图像? [英] How to add and show images in a WPF TreeView?
本文介绍了如何在WPF TreeView中添加和显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在代码中填写TreeView(WPF),并希望在项目中使用一些图标。
我可以加载并添加BitmapImage,但它只显示,当BitmapImage也分配给窗口中的另一个ImageControl(并显示在那里)时:
I fill a TreeView (WPF) in the code and want to use some icons in the items. I can load and add BitmapImage but it is only displayed, when the BitmapImage is also assigned to another ImageControl in the window (and shown there):
TreeViewItem newItem = new TreeViewItem();
Image tempImage = new Image();
BitmapImage bitmapImage = new BitmapImage(new Uri(@"/Resources/ok-01.png", UriKind.Relative));
tempImage.Source = bitmapImage;
imageControlInWindow.Source = bitmapImage; //if I delete this line (it is not needed) the image in the TreeViewItem is not shown
TextBlock tempTextBlock = new TextBlock();
tempTextBlock.Inlines.Add(tempImage);
tempTextBlock.Inlines.Add("SomeText");
newItem.Header = tempTextBlock;
如何强制图像显示在TreeView中而不会在树视图外显示图像作为副本?
How can I force the image to be shown in the TreeView without the hack of showing it outside the treeview as copy?
推荐答案
您没有从正确的资源文件包URI 。
它应该如下所示:
var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Resources/ok-01.png"));
图像文件的构建操作必须设置为资源。
The Build Action of the image file must be set to Resource.
这篇关于如何在WPF TreeView中添加和显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文