Show Image in dynamically generated DataGrid.Columns in WPF

后端 未结 1 750
北恋
北恋 2021-01-22 00:18

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:



        
1条回答
  •  深忆病人
    2021-01-22 00:53

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

    0 讨论(0)
提交回复
热议问题