简体   繁体   English

添加图像在C#wpf项目中以编程方式执行DataGrid - 如何?

[英]Adding image do DataGrid programically in C# wpf project - how?

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. 然后我想添加4列,例如3个文本和1个图像。 So at first I need to create a DataTable and DataRow with sample data: 所以首先我需要创建一个包含样本数据的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? 为什么在最终网格上连续有System.Windows.Controls.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? 如何显示真实图像而不是该字符串?

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. 最重要的是DataGrid(或GridView)不支持Image。 Why? 为什么? Don't ask me. 不要问我。 So i changed image to BitmapImage and it works like a charm. 所以我将图像更改为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). 但我需要将资源添加到XAML(不知道如何编程)。 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>

And all is working FINE! 一切都很好!

The same for GridView or ListView. GridView或ListView也是如此。 Hope it helps someone. 希望它可以帮助某人。

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" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM