WPF添加datagrid图像列可能吗?

使用C#.Net 4.5,Visual Studio 2012 Ulti,WPF。

我有一些旧的win-forms代码,我想在这个新的WPF应用程序中做。

代码如下:

DataGridViewImageCell pNew = new DataGridViewImageCell(); ParetoGrid.Columns.Add(new DataGridViewImageColumn() { CellTemplate = pNew, FillWeight = 1, HeaderText = "pNew", Name = "pNew", Width = 30 }); ParetoGrid.Columns["pNew"].DisplayIndex = 18; 

3行代码添加可以处理图像的列。 在WPF中,我看到它有点不同。 我需要添加“图像列”吗? 或者WPF列是否支持图像? 还是有另外3个线性解决方案,只是不同的语法?

谢谢你的帮助

看到这个答案:

WPF DataGrid中的图像列

         

要在以下代码中添加列:

 DataGridTextColumn textColumn1 = new DataGridTextColumn(); textColumn1.Header = "Your header"; textColumn1.Binding = new Binding("YourBindingField"); dg.Columns.Add(textColumn1); 

使用DataGridTemplateColumn添加自定义列请参阅: 如何以编程方式在wpf datagrid列中显示图像?

这是MainWindow.xaml的代码,只是为了更好地理解

`

       

在它之后是我的MainWindow.xaml.cs的图像或文本代码,用于动态添加datadrid …

 using System; using System.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Data; using System.Windows.Media.Imaging; using System.Windows.Controls; namespace Pic_in_Datagrid { ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public StudentData stu_data { get; set; } public MainWindow() { InitializeComponent(); stu_data = new StudentData(); List stu = new List(); stu.Add(new StudentData() { image = toBitmap(File.ReadAllBytes(@"D:\1.jpg")), stu_name = "abc" }); stu.Add(new StudentData() { image = toBitmap(File.ReadAllBytes(@"D:\1.jpg")), stu_name = "def" }); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.Image)); Binding bind = new System.Windows.Data.Binding("image");//please keep "image" name as you have set in your class data member name factory.SetValue(System.Windows.Controls.Image.SourceProperty, bind); DataTemplate cellTemplate = new DataTemplate() { VisualTree = factory }; DataGridTemplateColumn imgCol = new DataGridTemplateColumn() { Header = "image", //this is upto you whatever you want to keep, this will be shown on column to represent the data for helping the user... CellTemplate = cellTemplate }; dt1.Columns.Add(imgCol); dt1.Columns.Add(new DataGridTextColumn() { Header = "student name", Binding = new Binding("stu_name") //please keep "stu_name" as you have set in class datamember name }); dt1.ItemsSource = stu; } public static BitmapImage toBitmap(Byte[] value) { if (value != null && value is byte[]) { byte[] ByteArray = value as byte[]; BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(ByteArray); bmp.EndInit(); return bmp; } return null; } } public class StudentData { public BitmapImage image { get; set; } public string stu_name { get; set; } } } 

以上所有代码都来自不同的资源……感谢他们开发和共享这些代码……

这就是我做的。 使用像这样的图像控件在数据网格中添加数据窗口

         

正如你所看到的那样,我正在使用名为“FileIcon”的属性绑定Image,这个属性在类Version中使用

  public class Version { public string FileIcon { get; set; } } 

现在只需要这样做就是绑定提供“FileIcon”的路径并像这样更新DataGrid的ItemSource

  ObservableCollection items = new ObservableCollection(); items.Add(new Version() { FileIcon = "/AssemblyName;component/Images/eye.png", }); YourDataGrid.ItemsSource = null; YourDataGrid.ItemsSource = items;