Change DataGrid column header text

后端 未结 6 890
说谎
说谎 2021-01-11 23:26

I have a list of a specific class type Person and I want to make a DataGrid with it.

private void DataGrid_Loaded(object sender, Ro         


        
6条回答
  •  感情败类
    2021-01-12 00:07

    Here the Right way to do it :

    First Define an ObservableCollection in the codebehind that will hold a list of persons

    Second Bind that list to the DataGrid ItemSource and Bind its properties

    You can change what name to display on each column by simply disabling the AutoGenerateColumns and setting their names by your self

    here the full code

    
          
                
                
          
      
    

    and the code behind :

    public class Person
    {
        public String FName { get; set; }   
        public String LName { get; set; }   
    
    }
    public partial class MainWindow : Window
    {
        public ObservableCollection ListPersons { get; set; }
        public MainWindow()
        {
            ListPersons=new ObservableCollection()
            {
                new Person()
                {
                    FName = "FName1",
                    LName = "LName1"
                },
                 new Person()
                {
                    FName = "FName2",
                    LName = "LName2"
                }
    
            };
            this.DataContext = this;
    
        }
    
    
    }
    

提交回复
热议问题