在WPF中动态生成的DataGrid.Columns中显示图像 [英] Show Image in dynamically generated DataGrid.Columns in WPF

查看:364
本文介绍了在WPF中动态生成的DataGrid.Columns中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须透视查询中的信息数据,并根据从底层数据库读取的值显示图像。

I've to pivot information data from a query, and show images based on value read from the underlying database.

假设我没有这些数据查询:

Let's say I have this data out of my query:

Identifiant|ProcessId|AlarmLevel
  BOUDA25  |   100   |    1
  BOUDA25  |   110   |    1
  BOUDA25  |   130   |    1
  BOUDA25  |   205   |    2
  BOUDA25  |   210   |    2

我现在要使以下WPF DataGrid显示由 images / circle_orange.ico 等。

I now want to make the following WPF DataGrid display the actual images represented by images/circle_orange.ico, etc.

到目前为止,我的代码类似于

So far my code resembles this:

private void PopulateGrid(IEnumerable<AlarmeViewModel> alarmes) {        
    // Used to pivot the information data
    var table=new DataTable();

    // Generates the columns based on rows
    table.Columns.Add("Identifiant");
    table.Columns.Add("=>");

    alarmes.ForEach(a => a.Processes.ForEach(pid => table.Columns.Add(columnName:pid, type typeof(string))));

    // Generates the rows
    var images = new string[] {
        "images/circle_grey.ico",
        "images/circle_orange.ico",
        "images/circle_yellow.ico",
        "images/circle_red.ico",
        "images/circle_green.ico"        
    };

    alarmes.ForEach(a => {
        var row = table.NewRow();
        row[0] = a.Identifiant

        for(int i=0; i<a.Niveaux.Count; row[a.Processes[i]]=images[a.AlarmLevel[1]], i++);

        table.Rows.Add(row);
    });

    // Refreshes the DataGrid content
    alarmDataGrid.BeginInit();
    alarm.DataGrid.ItemsSource=table.DefaultView;
    alarmDataGrid.EndInit();
}

现在我停留三天了,无法通过ImageSource显示这些图像

Now I'm stuck since three days to make those image display through an ImageSource binding.

我试图让DataGrid自行自动生成列,并且还尝试通过将其添加到代码中来解决此问题,这些代码来自该问题的可接受答案:

I tried to let the DataGrid autogenerate the columns by itself, and I also tried by adding them in the code behind from the accepted answer of this question:

这也是:

我就是不明白。我知道我已经接近了,但我想我仍然很明显。

And I just can't get it. I know I'm close, but still missing the obvious, I guess.

推荐答案

您可以处理 AutoGeneratingColumn 事件并以编程方式创建一个 DataGridTemplateColumn ,其中包含一个 Image 元素。试试这个:

You could handle the AutoGeneratingColumn event and programmatically create a DataGridTemplateColumn that contains an Image element. Try this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName != "Identifiant" && e.PropertyName != "=>")
    {
        FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
        image.SetBinding(Image.SourceProperty, new Binding(e.PropertyName));

        e.Column = new DataGridTemplateColumn
        {
            CellTemplate = new DataTemplate() { VisualTree = image },
            Header = e.PropertyName
        };
    }
}

这篇关于在WPF中动态生成的DataGrid.Columns中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆