Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show image in wpf datagrid column programmatically?

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

like image 693
dee Avatar asked Oct 25 '25 10:10

dee


1 Answers

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.

like image 124
viky Avatar answered Oct 28 '25 05:10

viky