© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
59 replies
grimpows

Dynamic Binding for DataGrid WPF

following this article : https://paulstovell.com/dynamic-datagrid/
i'm stuck at the "Rendering basic columns in a DataGrid" section.

I dont really see where i should put that code in a MVVM practice.
should i create the entire datagrid in the VM ?
should i trigger on some ItemsSource updated event in the code behind ? ...

fixed with help of https://stackoverflow.com/questions/64687172/how-to-autogenerate-datagrid-columns-for-collection-of-child-type-from-collectio

first i do create a Data class that contain my Records :
//the BindableBase is from prism but is same as implementation of INotifyPropertyChanged
public class Data : BindableBase
    {
        private ObservableCollection<Record> _records = new();
        public ObservableCollection<Record> Records
        {
            get { return _records; }
            set { 
                SetProperty(ref _records, value);
            }
        }
    }
//the BindableBase is from prism but is same as implementation of INotifyPropertyChanged
public class Data : BindableBase
    {
        private ObservableCollection<Record> _records = new();
        public ObservableCollection<Record> Records
        {
            get { return _records; }
            set { 
                SetProperty(ref _records, value);
            }
        }
    }


record :
public class Record
    {
        private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

        public Record(params Property[] properties)
        {
            foreach (var property in properties)
                Properties.Add(property);
        }

        public ObservableCollection<Property> Properties
        {
            get { return properties; }
        }


    }
public class Record
    {
        private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

        public Record(params Property[] properties)
        {
            foreach (var property in properties)
                Properties.Add(property);
        }

        public ObservableCollection<Property> Properties
        {
            get { return properties; }
        }


    }


property
public class Property 
    {
        public Property(string name, object value)
        {
            Name = name;
            Value = value;
        }

        public string Name { get; private set; }
        public object Value { get; set; }

    }
public class Property 
    {
        public Property(string name, object value)
        {
            Name = name;
            Value = value;
        }

        public string Name { get; private set; }
        public object Value { get; set; }

    }


in your VM you can instanciate your prop with

public class SomeViewModel: ViewModelBase
{
  private Data _data = new();
  public Data Data
  {
    get { return _data; }
    set { SetProperty(ref _data, value); }
  }
}
public class SomeViewModel: ViewModelBase
{
  private Data _data = new();
  public Data Data
  {
    get { return _data; }
    set { SetProperty(ref _data, value); }
  }
}
Paul Stovell's Blog
WPF Dynamically Generated DataGrid
Sometimes you might have a very dynamic source of data, with classes to represent rows and properties. Here's how you can use a WPF DataGrid with it.
WPF Dynamically Generated DataGrid
Stack Overflow
How to autogenerate DataGrid columns for collection of child type f...
So I have a bunch of classes that are derived from some base class. I have classes (collectors) that have a methods that returns collections of these classes. I also have a TabControl where each ta...
How to autogenerate DataGrid columns for collection of child type f...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

❔ WPF Dynamic DataGrid
C#CC# / help
3y ago
Binding error Datagrid WPF
C#CC# / help
2y ago
Binding Dictionary to DataGrid (WPF)
C#CC# / help
2y ago
WPF binding problem in datagrid header
C#CC# / help
2y ago