I have a File class defined as following
public class File
{
public string FileName {set;get;}
public List<Property> PropertyList;
}
Here is how Property class looks like:
public class Property
{
public string PropertyName { set; get;};
public string PropertyValue { set; get;};
...
}
I need to bind a List<File> to a DataGrid, display FileName. Also, I want to create a column for each Property in the PropertyList,
with the string value of PropertyName as the column title, and string value of PropertyValue as the value of the column.
Is this possible in WPF?
Thanks,
Just had to try this one, odd but funny problem:-) Managed to get it working by using the following.
Sorry for the long answer, probably way to detailed 🙂
First of, the DataGrid. Plain and simple
Then the File class, named MyFile
Property class, named MyProperty
A list containing MyFiles
Created some dummy data to populate the list and set the ItemsSource on the DataGrid
Added a DataGridTextColumn for the FileName attribute
And then for the MyPropertyList. I only wanted to add each MyPropertyName once so if I have the following
MyFile1
-Name1
-Name2
-Name3
MyFile2
-Name1
-Name4
-Name5
the generated columns shall be Name1, Name2, Name3, Name4 and Name5.
I had to feed the MyPropertyName to the constructor of the Converter so it would know which property to look for.
And finally the Converter
In Convert it will check for the given MyPropertyName and if it finds it, return the MyPropertyValue, otherwise null. The same goes for the ConvertBack method but it will set MyPropertyValue to the new value for the MyProperty with the given MyPropertyName and then return the list which is either a list or null.
The properties that aren’t in a MyFile list won’t be editable, they will just change back to null upon leaving the cell (which is probably the point).
This will result in a DataGrid that looks like this.