DataGridTemplateColumn

DataGridTemplateColumn

本文介绍了如何以编程方式在wpf datagrid列中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在wpf datagrid中添加两列,一个图像&一个文本列动态。



Xaml代码:

 < Grid> ;< DataGrid AutoGenerateColumns =FalseHeight =AutoHorizo​​ntalAlignment =StretchMargin =0Name =gridVerticalAlignment =StretchWidth =Auto>< / DataGrid>< / Grid> ; 

代码背后:

  DataGridTextColumn col = new DataGridTextColumn(); 
col.Header = Text1;
col.Binding = Text1;
grd.Columns.Add(col);

如何添加图像列?或在列中显示图像?



请建议



Dee

解决方案

如Anvaka所说,您可以使用 DataGridTemplateColumn
在C#中,您可以添加创建 DataGridTemplateColumn 作为这一点,这里我添加了一个 CheckBox code> 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 =新的DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);

这里图片是 ImageSource 类型的属性在该集合被分配到 ItemsSource DataGrid


I want to add two columns in wpf datagrid one image & one text columns dynamically.

Xaml code :

 <Grid><DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="grid" VerticalAlignment="Stretch" Width="Auto" ></DataGrid></Grid>

Code Behind:

 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?

Please suggest

Dee

解决方案

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);

Here Picture is a property of ImageSource type in the class which collection is assigned to ItemsSource of DataGrid.

这篇关于如何以编程方式在wpf datagrid列中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 16:31