I have a collection of objects stored in a CollectionViewSource and bound to a DataGrid. I want to display a ‘detail view’ of the object currently selected in the DataGrid. I can obtain the current object using CollectionViewSource.View.CurrentItem.
MyClass
{
[IsImportant]
AProperty{}
AnotherProperty{}
[IsImportant]
YetAnotherProperty{}
}
What I would like to do is display a label (with the property name) and a control (for editing) in a listbox, for each of those properties marked with the IsImportant attribute. The binding must work between the edits made, the DataGrid and the backing object. The control displayed should vary based on the property’s type, which can either be boolean, string or IEnumerable<string> (I have written an IValueConverter to convert between enumerable and newline-delimited string).
Does anyone know of a method for accomplishing this? I can currently display the values of each property through the following, but editing them would not update the backing object:
listBox.ItemsSource = from p in typeof(MyClass).GetProperties()
where p.IsDefined(typeof(IsImportant), false)
select p.GetValue(_collectionViewSource.View.CurrentItem, null);
To clarify, I would like this to happen ‘automagically’, without manually specifying property names in the XAML. If I can dynamically add to the XAML at runtime based on which properties are marked with attributes, that would also be fine.
You want a control that has a label with the property name and control to edit the property value, so start by creating a class that wraps a property of a specific object to act as the DataContext for that control:
You want to bind the ItemsSource of a ListBox to an object in order to populate it with these controls, so create an IValueConverter that will convert an object to a list of PropertyValue objects for its important properties:
The final trick is that you want the edit control to vary based on the property’s type. You can do that by using a ContentControl and setting the ContentTemplate to one of various editor templates based on the property type. This example uses a CheckBox if the property is a Boolean and a TextBox otherwise:
Then, you can just create your ListBox as: