Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Autogenerate and bind datagrid columns according to nested property of item in observable collection

$
0
0

Hello!:)

Im developing WPF app using MVVM pattern and I would like to make my own generic datagrid control for this project, where i am going to use this datagrid a lot, but with different datagrid items -> dynamic columns. So this items should have some common properties e.g. rowcolor etc., so i thought of making wrapping object which will have these common properties and generic datagrid item like this:

class DataGridItem<T>
{
    public Visibility Visibility { get; set; }
    public SolidBrush Color { get; set; }
    public T Item { get; set; }
}

The T type can be like this:

class MyItem
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age { get;set; }
}

Then my observable collection for datagrid will be like:

public ObservableCollection<DatagriItem<MyItem>> { get;set; }

So I am curious if i can make that datagridautogenerating columns will generate columns from properties of MyItem object instead of dataGridItem object. So my columns will be FirstName, LastName, Age. I like this wrapping because this wasy i can prevent from changing my common properties from outside. Can anybody help me or suggest different way how to achieve generic datagrid control? I prefer if the item will be fully separated from DataGridItem, because i want to make datagrid control as my own library a then use it in project(maybe not onyl that one project). Thanks very much for your time and also for your help:) See ya



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>