【发布时间】:2015-10-14 06:59:33
【问题描述】:
我在代码中填写了一个 TreeView (WPF),并希望在项目中使用一些图标。 我可以加载和添加 BitmapImage,但它仅在 BitmapImage 也分配给窗口中的另一个 ImageControl(并显示在那里)时才会显示:
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 中显示,而无需将其作为副本显示在 Treeview 之外?
【问题讨论】:
-
为什么不为树视图项目建立数据模板?
-
InvalidateVisual怎么样 -
图像文件的构建操作应设置为资源。然后它将由 Resource File Pack URI:
new Uri("pack://application:,,,/Resources/ok-01.png")加载。 -
感谢 Clemens ... 成功了 :-) 想要将此添加为正确答案吗?非常感谢! :-)
标签: c# wpf image treeview bitmapimage