Here is the code example given to add Image column in Datagrid control dynamically at run time in WPF.
public void AddColumnsToDataGrid(DataGrid dataGrid, DataGridColumnType columnType, string headerText, string bindingName, string imagePath,
)
{
FrameworkElementFactory factoryElementStackPanelImages = AddMultipleImagesIntoOneColumn(imagePath);
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = factoryElementStackPanelImages;
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Header = headerText;
dgTemplateColumn.CellTemplate = dataTemplate;
dgTemplateColumn.CellStyle = styleDataGridCell;
dataGrid.Columns.Add(dgTemplateColumn);
}
private FrameworkElementFactory AddMultipleImagesIntoOneColumn(string imagesSourcePath)
{
FrameworkElementFactory factoryElementStackPanel = new FrameworkElementFactory(typeof(StackPanel));
try
{
string imagePath, imageName;
factoryElementStackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory factoryImage;
if (imagesSourcePath.Contains(ConstantVariables.CharacterValueHash))
{
string[] arrImageSource = imagesSourcePath.Split(Convert.ToChar(ConstantVariables.CharacterValueHash));
foreach (string imgSource in arrImageSource)
{
if (imgSource.Contains(ConstantVariables.CharacterValueDollar))
{
string[] arrImagePathNName = imgSource.Split(Convert.ToChar(ConstantVariables.CharacterValueDollar));
imagePath = arrImagePathNName[0];
imageName = arrImagePathNName[1];
}
else
{
imagePath = imgSource;
imageName = string.Empty;
}
Uri uri = new Uri(ConstantVariables.UriString + imagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
factoryImage = new FrameworkElementFactory(typeof(Image));
factoryImage.SetValue(Image.SourceProperty, bitMapSource);
factoryImage.SetValue(Image.WidthProperty, 15d);
factoryImage.SetValue(Image.HeightProperty, 15d);
factoryImage.SetValue(Image.MarginProperty, new Thickness(1));
if (!imageName.Equals(string.Empty))
{
factoryImage.SetValue(Image.NameProperty, imageName);
factoryImage.SetValue(Image.ToolTipProperty, imageName);
}
factoryElementStackPanel.AppendChild(factoryImage);
ImageColumnElement.Add(factoryImage);
}
}
else
{
if (imagesSourcePath.Contains(ConstantVariables.CharacterValueDollar))
{
string[] arrImagePathNName = imagesSourcePath.Split(Convert.ToChar(ConstantVariables.CharacterValueDollar));
imagePath = arrImagePathNName[0];
imageName = arrImagePathNName[1];
}
else
{
imagePath = imagesSourcePath;
imageName = string.Empty;
}
Uri uri = new Uri(ConstantVariables.UriString + imagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
factoryImage = new FrameworkElementFactory(typeof(Image));
factoryImage.SetValue(Image.SourceProperty, bitMapSource);
factoryImage.SetValue(Image.WidthProperty, 15d);
factoryImage.SetValue(Image.HeightProperty, 15d);
factoryImage.SetValue(Image.MarginProperty, new Thickness(1));
if (!imageName.Equals(string.Empty))
{
factoryImage.SetValue(Image.NameProperty, imageName);
factoryImage.SetValue(Image.ToolTipProperty, imageName);
}
factoryElementStackPanel.AppendChild(factoryImage);
ImageColumnElement.Add(factoryImage);
}
}
catch (Exception ex)
{ LogExceptionDetails(ex); }
return factoryElementStackPanel;
}