I am trying to use WPF's Datagrid to display the content of an object. Usually, this is very simple when you are trying to display something when the class is like this :
class Employee
{
public string Name { get; set; }
public string Phone { get; set; }
public bool Active { get; set; }
}
However, let say you have something more complex like this :
class Employee
{
public Employee()
{
SecurityAccesses = new public Dictionary<string, bool>();
}
public string Name { get; set; }
public string Phone { get; set; }
public bool Active { get; set; }
public Dictionary<string, bool> SecurityAccesses { get; }
}
If you assign this to ItemSource, DataGrid will not know what to do with SecurityAccesses and will only display a column named SecurityAccesses and the cell will show as a collection.
What I would like to do is make the Datagrid aware that it should get all the keys of the dictionary to display the column names and of course the values to be displayed as a checkbox inside the datagrid.
I could do this with some code behind but I am trying to use XAML as much as possible so is there a way with behaviors that this could be done. Your recommendations would be really appreciated.
Your DataGrid can be bound to an observable collection employees. Then your employees DataGrid can contain a DataGridTemplateColumn that contains a (child) DataGrid bound to the SecurityAccesses property of each employee. The data context of the child DataGrid will be the single employee bound to the parent DataGrid's row. Something like: