如何以编程方式在 wpf datagrid 列中显示图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:21:50  来源:igfitidea点击:

How do I show image in wpf datagrid column programmatically?

wpfimagedatagrid

提问by dee

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

我想在 wpf datagrid 中动态添加两列一个图像和一个文本列。

Xaml code :

Xml代码:

 <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

回答by viky

As Anvaka said, you can Use DataGridTemplateColumn. In C# you can add create DataGridTemplateColumnas this, Here i have added a CheckBoxin to the DataGridTemplateColumn.

正如 Anvaka 所说,您可以使用DataGridTemplateColumn. 在 C# 中,您可以DataGridTemplateColumn像这样添加 create ,这里我CheckBoxDataGridTemplateColumn.

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 ImageSourcetype in the class which collection is assigned to ItemsSourceof DataGrid.

这里 Picture 是ImageSource类中的一个属性,该类中的集合被分配给ItemsSourceof DataGrid

回答by Anvaka

Use DataGridTemplateColumn. Define cell template in Window.Resourcesand use FindResource()to set column's CellTemplateproperty.

使用DataGridTemplateColumn。在中定义单元格模板Window.Resources并使用FindResource()设置列的CellTemplate属性。

Hope this helps.

希望这可以帮助。

回答by Eros Guil

If you want to Set an Image in a DataGrid Column HEADER, only programmatically, you can perform like this:

如果你想在 DataGrid Column HEADER 中设置一个图像,只能以编程方式,你可以这样执行:

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

您可以将此方法用于任何类型(例如:TextBlock、Label、...),或者创建更复杂的 controlTemplate