繁体   English   中英

在 wpf 数据网格列中未显示在后面的代码中设置图像

[英]Setting Image in code behind is not showing up in wpf datagrid column

我有一个数据模板来在数据网格列中显示图标。 我在后面的代码中设置了图像源。 但不知何故,图像在运行时没有出现在网格中。 我错过了什么吗??

<DataTemplate x:Key="iconTemplate">          
  <Image/>
</DataTemplate>

背后的代码

var cellTemplate = (DataTemplate)Resources["iconTemplate"];
var image = cellTemplate.LoadContent() as Image;

image.Source = new BitmapImage(new Uri(@"C:\images\16x16\image.png"));
column.CellTemplate = cellTemplate;

在 xaml 中设置 datagrid 模板列的单元格模板。

<DataGrid.Columns>
  <DataGridTemplateColumn Header="Comments" CellTemplate="{StaticResource iconTemplate}"/>
</DataGrid.Columns>

LoadContent在这里没有帮助:

将模板的内容作为 object 的实例加载并返回内容的根元素。

返回值

类型:System.Windows.DependencyObject
内容的根元素。 多次调用它会返回单独的实例。

此外,您不能修改 DataTemplate,因为它们一旦使用就会被密封。 您可以将 DataTemplate 作为 DynamicResource 引用,并在运行时完全替换旧模板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM