如何以编程方式在 wpf datagrid 列中显示图像? [英] How do I show image in wpf datagrid column programmatically?
本文介绍了如何以编程方式在 wpf datagrid 列中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 wpf datagrid 一个图像中添加两列 &一个文本列动态.
I want to add two columns in wpf datagrid one image & one text columns dynamically.
XML 代码:
<Grid><DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="grid" VerticalAlignment="Stretch" Width="Auto" ></DataGrid></Grid>
背后的代码:
DataGridTextColumn col = new DataGridTextColumn();
col.Header =Text1;
col.Binding =Text1;
grd.Columns.Add(col);
如何添加图片列?或在列中显示图片?
How do I add image column?or show image in the column?
请推荐
迪
推荐答案
正如 Anvaka 所说,您可以使用 DataGridTemplateColumn
.在 C# 中,你可以添加 create DataGridTemplateColumn
作为这个,这里我添加了一个 CheckBox
到 DataGridTemplateColumn
.
As Anvaka said, you can Use DataGridTemplateColumn
.
In C# you can add create DataGridTemplateColumn
as this, Here i have added a CheckBox
in to the DataGridTemplateColumn
.
DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("Picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);
这里的图片是DataGrid
的ItemsSource
类中ImageSource
类型的属性.
Here Picture is a property of ImageSource
type in the class which collection is assigned to ItemsSource
of DataGrid
.
这篇关于如何以编程方式在 wpf datagrid 列中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文