WPF Datagrid binding custom column headers

后端 未结 3 1602
一个人的身影
一个人的身影 2021-02-03 12:22

I am trying to figure out how to bind a WPF DataGrid\'s column header and main data to a data source using an MVVM pattern. The result I\'m looking for would look like this:

3条回答
  •  心在旅途
    2021-02-03 13:02

    I am definitely a WPF / MVVM / databinding noob, but have been working hard on this stuff lately. I don't know what you have wired up so far, but first you'll want to set the DataContext for your View. Since you're using MVVM, I assume you have a ViewModel, so that should be the DataContext for your View.

    i.e. if you have your View create / own your ViewModel, it could look something like this:

    MyViewModel vm = new MyViewModel();
    this.DataContext = vm;
    

    You can easily databind your CheckBox, ComboBox, and TextBox to properties in your ViewModel. I have found the easiest way is to make your ViewModel inherit from a base viewmodel class, like the one that Josh Smith wrote. This will give you a method to call internally when you want the ViewModel to notify the GUI of any changes in values.

    Assuming you have properties like ImportColumn, LastName, and LastNameText (all C# properties, not fields that call OnPropertyChanged accordingly), then your XAML would look something like this:

    
    
    
    

    I hope this helps you out. If not, please comment and I'll try to do as best as I can to try other things out.

提交回复
热议问题