How to programmatically display image in datagrid wpf column?

I want to add two columns to the wpf datagrid for one image and one text column dynamically.

Xaml Code:

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

Code for:

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

How to add image column or show image in column?

Please suggest

Dee

+2
source share
2 answers

As Anwaka said, you can use DataGridTemplateColumn. In C # you can add create DataGridTemplateColumnlike this, here I added CheckBoxin 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 type property ImageSourcein the class to which the collection is assigned ItemsSource DataGrid.

+4
source

DataGridTemplateColumn. Window.Resources FindResource() CellTemplate.

, .

0

All Articles