How do I show image in wpf datagrid column programmatically?

前端 未结 3 678
春和景丽
春和景丽 2020-12-11 22:33

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

Xaml code :

 

        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 22:36

    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

提交回复
热议问题