WPF add datagrid image column possible?

前端 未结 4 1072
抹茶落季
抹茶落季 2020-12-17 18:38

Using C#.Net 4.5, Visual Studio 2012 Ulti, WPF.

I\'ve got some old win-forms code that i wanted to do in this new WPF app.

code is the following:

<         


        
4条回答
  •  Happy的楠姐
    2020-12-17 19:25

    Here is what I did. Add a datatemplate in your datagrid with image control like this

                
                    
                        
                            
                        
                    
                
    

    As you can see my I am binding Image with a property named "FileIcon" that is used in class Version like this

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

    Now only this you have to do is bind provide a path to "FileIcon" and update ItemSource of DataGrid like this

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

提交回复
热议问题