在C#wpf项目中,DataGrid以编程方式添加图像-怎么做? [英] Adding image do DataGrid programically in C# wpf project - how?
问题描述
我有问题.
我想在没有VS Designer的情况下用C#编程编写所有内容.
I want to write ALL things programically in C#, without VS Designer.
因此,我正在创建图像和DataGrid(并将其添加为MainWindow Grid的子级):
So, I'm creating an image and and DataGrid (and I'm adding it as a child of MainWindow Grid):
Image img = new Image();
Uri uri = new Uri(@"C:\d1.jpg");
img.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
DataGrid dg = new DataGrid();
grid1.Children.Add(dg);
然后我想添加4列,例如3列文本和1列图像.因此,首先我需要使用示例数据创建一个DataTable和DataRow:
Then I want to add 4 columns for example, 3 of text and one of image. So at first I need to create a DataTable and DataRow with sample data:
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Columns.Add("Column4", typeof(Image)); // type of image!
DataRow dr = dt.NewRow();
dr[0] = "aaa";
dr[1] = "bbb";
dr[2] = "ccc";
dr[3] = img; // add a sample image
dt.Rows.Add(dr);
现在我有一个包含4列和1行数据的DataTable.
Now I have a DataTable with 4 columns and 1 row of data.
然后我要做的就是像这样设置DataGrid的ItemsSource:
Then all I need to do is to set ItemsSource of DataGrid like this:
dg.ItemsSource = dt.DefaultView;
我做错了什么?为什么在最后一个网格上连续有System.Windows.Controls.Image而不是真实图像?我需要绑定它吗?
What I'm doing wrong? Why on the final grid there is System.Windows.Controls.Image in a row instead of real image? Do I need to bind it or something?
没有设计人员,我需要以编程方式做的所有事情.
All things I need to do programically, without designer.
如何显示真实图像而不是该字符串?
How to display real image instead of that string?
推荐答案
仍然不知道如何以编程方式100%完成该操作,但我知道了.
Still don't know how to do it in 100% programically, but i figure it out.
最重要的是,DataGrid(或GridView)不支持Image.为什么?不要问我因此,我将图像更改为BitmapImage,它的工作原理就像一种魅力.
The most important thing is that DataGrid (or GridView) doesn't support Image. Why? Don't ask me. So i changed image to BitmapImage and it works like a charm.
所以有我的修改:
Uri uri = new Uri(@"C:\d1.jpg");
BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(uri);
...
DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = false;
DataGridTemplateColumn col1 = (DataGridTemplateColumn)this.FindResource("dgt");
dg.Columns.Add(col1);
DataGridTextColumn col2 = new DataGridTextColumn();
col2.Header = "Column2";
col2.Binding = new Binding("Column2");
dg.Columns.Add(col2);
...
DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(BitmapImage));
dt.Columns.Add("Column2");
DataRow dr = dt.NewRow();
dr[0] = bmp;
dr[1] = "test";
dt.Rows.Add(dr);
...
dg.ItemsSource = dt.DefaultView;
grid1.Children.Add(dg);
但是我需要将资源添加到XAML(不知道如何编程).所以有一个代码:
But i needed to add Resources to XAML (don't know how to program it). So there is a code:
<Window.Resources>
<DataGridTemplateColumn x:Key="dgt" Header="Column1" Width="SizeToCells">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Column1}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</Window.Resources>
一切都很好!
与GridView或ListView相同. 希望对别人有帮助.
The same for GridView or ListView. Hope it helps someone.
这篇关于在C#wpf项目中,DataGrid以编程方式添加图像-怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!