I want to add two columns in wpf datagrid one image & one text columns dynamically.
Xaml code :
If you want to Set an Image in a DataGrid Column HEADER, only programmatically, you can perform like this:
ImageSource image = new BitmapImage(new Uri(@"C:/téléchargement.jpg", UriKind.RelativeOrAbsolute));
Style style = new Style(typeof(DataGridColumnHeader));
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Image));
factory.SetValue(Image.SourceProperty, image);
factory.SetValue(Image.StretchProperty, Stretch.Uniform);
style.Setters.Add(new Setter { Property = TemplateProperty, Value = new ControlTemplate { TargetType = typeof(DataGridColumnHeader), VisualTree = factory } });
DataZone.Columns[5].HeaderStyle = style;
You can use this method for any type ( Ex : TextBlock , Label, ...), or create a more complex controlTemplate