C# 在Datagrid中添加图像

C# 在Datagrid中添加图像,c#,wpf,datagrid,C#,Wpf,Datagrid,这是我的datagrid我想在网格列中显示图像,但它显示的是文本System.Windows.Media.Imaging.BitmapImage,而不是图像。 如何在datagrid列中添加图像 这就是我尝试过的: Dictionary dict1=new Dictionary < string,object>(); string keyimage="Image"; object a1 = new BitmapImage(); a1=Imaging.CreateBitmapSo

这是我的datagrid我想在网格列中显示图像,但它显示的是文本
System.Windows.Media.Imaging.BitmapImage
,而不是图像。 如何在datagrid列中添加图像

这就是我尝试过的:

Dictionary dict1=new Dictionary < string,object>();

string keyimage="Image";

object a1 = new BitmapImage();

a1=Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Information.Handle, Int32Rect.Empty, null);

dict1.Add(keyimage, a1);
Dictionary dict1=新字典();
字符串keyimage=“Image”;
对象a1=新的位图图像();
a1=Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Information.Handle,Int32Rect.Empty,null);
1.添加(键图像,a1);

dict1
用作数据网格的
Itemsource
……如何使图像显示在数据网格列中?

我假设您的列数据类型与Itemsource数据类型不匹配。您必须为网格列和itemsource创建相等的数据类型映像。 在我的WPF项目中,我使用图像制作了我的列数据模板:

DataTable dtab = new DataTable();
dtab.Columns.Add("imageColumn", typeof(BitmapImage)); 
//Uploading dtab with Images from DataBase

FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Image));
Binding bind = new System.Windows.Data.Binding("imageColumn");
factory.SetValue(Image.SourceProperty, bind);
DataTemplate cellTemplate = new DataTemplate() { VisualTree = factory };
DataGridTemplateColumn imgCol = new DataGridTemplateColumn() 
{
  Header="image",
  CellTemplate = cellTemplate
};
DataGrid dg = new DataGrid();
dg.Columns.Add(imgCol);
dg.ItemsSource = dtab.DefaultView;

希望此帮助将任何图像添加到资源文件夹上的项目中

之后,在DataGrid绑定事件中尝试下面的编码。在这里我添加了图像标志

DataGridViewImageColumn imageColoumn = new DataGridViewImageColumn();
        imageColoumn.HeaderText = "Img";
        imageColoumn.Image = null;
        imageColoumn.Name = "DataPic";
        imageColoumn.Width = 150;
        dataGridView1.Columns.Add(imageColoumn);

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewImageCell cell = row.Cells[1] as DataGridViewImageCell;
            cell.Value = (System.Drawing.Image)Properties.Resources.logo;
        }

确保使用
DataGridTemplateColumn
并将图像放入其中

由于您正在使用字典,请绑定,类似于:

<Image Binding="{Binding Height="25" Width="50" Source="{Binding Value}" />

你用什么列类型来显示图像?这是我的问题…………我应该用什么列类型来显示图像?你在这本词典中会有什么,仅图像?不只是图像。我只想在c#代码中的datagrid中添加一个图像列,而不是在xamlDataGridViewImageColumn中。它是否有任何名称空间或其他内容?我使用的是wpf datagrid而不是datagridviewok,然后您只需尝试此链接。。。我已经设置了AutoGenerateColumns=“True”,因为我有一个非常好的数据集,我可以轻松地将其与datagrid绑定,但现在我正在尝试在数据集中添加一个datadrid未显示的图像……xaml中没有任何更改我唯一能做的就是在c代码中做一些事情。。。。。。。。。。