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.