在 C# wpf 项目中以编程方式添加图像做 DataGrid - 如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13531327/
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
Adding image do DataGrid programically in C# wpf project - how?
提问by miecio1998
I have a problem.
我有个问题。
I want to write ALL things programically in C#, without VS Designer.
我想在没有 VS Designer 的情况下用 C# 以编程方式编写所有内容。
So, I'm creating an image and and DataGrid (and I'm adding it as a child of MainWindow Grid):
因此,我正在创建一个图像和 DataGrid(并将其添加为 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);
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:
然后我想添加 4 列,例如 3 列文本和 1 列图像。因此,首先我需要使用示例数据创建一个 DataTable 和 DataRow:
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);
Now I have a DataTable with 4 columns and 1 row of data.
现在我有一个包含 4 列和 1 行数据的 DataTable。
Then all I need to do is to set ItemsSource of DataGrid like this:
然后我需要做的就是像这样设置 DataGrid 的 ItemsSource:
dg.ItemsSource = dt.DefaultView;
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?
我做错了什么?为什么在最后的网格上有一行 System.Windows.Controls.Image 而不是真实图像?我需要绑定它还是什么?
All things I need to do programically, without designer.
我需要以编程方式完成的所有事情,无需设计师。
How to display real image instead of that string?
如何显示真实图像而不是那个字符串?
回答by miecio1998
Still don't know how to do it in 100% programically, but i figure it out.
仍然不知道如何以编程方式 100% 做到这一点,但我想通了。
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.
最重要的是DataGrid(或GridView)不支持Image。为什么?不要问我。所以我将图像更改为 BitmapImage,它的作用就像一个魅力。
So there is my modifications:
所以有我的修改:
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);
But i needed to add Resources to XAML (don't know how to program it). So there is a code:
但我需要将资源添加到 XAML(不知道如何编程)。所以有一个代码:
<Window.Resources>
<DataGridTemplateColumn x:Key="dgt" Header="Column1" Width="SizeToCells">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Column1}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</Window.Resources>
And all is working FINE!
一切正常!
The same for GridView or ListView. Hope it helps someone.
GridView 或 ListView 也是如此。希望它可以帮助某人。
回答by Rodolfo Gaspar
here has a super simple example that helped me so much
这里有一个超级简单的例子,对我帮助很大
http://www.c-sharpcorner.com/Forums/Thread/80586/displaying-images-in-datagrid-in-wpf-C-Sharp.aspx
http://www.c-sharpcorner.com/Forums/Thread/80586/displaying-images-in-datagrid-in-wpf-C-Sharp.aspx
I adapted the sample code for the MVVM
我修改了 MVVM 的示例代码
View
看法
<DataGrid x:Name="dgGrid" ItemsSource="{Binding collection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="25" Width="50" Source="{Binding path}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding name}"/>
</DataGrid>
Entity
实体
public class File
{
public string path{ get; set; }
public string name{ get; set; }
}
ViewModel
视图模型
var collection = new ObservableCollection<File>();
collection.Add(new File() { path=@"C:\Users\homeIcon.png", name="Home" });

