Binding MVVM with (only) some columns autogenerated from a collection
I have a collection of objects which I开发者_Python百科 want to bind to a RadGridView (from the toolkit telerik). The class of the objects is looking like that (minimum code needed to understand) where I have 1 property and 1 array of values which :
public class AttributeEntry : INotifyPropertyChanged
{
public string Code { get; set; }
private string[] _values;
public string[] Values
{
get { return _values; }
set { _values = value; }
}
public string this[int index]
{
get { return _values[index]; }
set
{
_values[index] = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Is someone know a (simple) way, using the patern MVVM, to have a RadGridView with some columns defined (in my case corresponding to the 'Code' property) and some columns "autogenerated" with each columns corresponding to the elements of a collection (in my case the elements of the array 'Values". If I have 7 values in my collection, I should have 7 "autogenerated" columns)?
I'm assuming you are binding your RadGridView
to a collection of AttributeEntry
.
In that case, implement ITypedList
on your collection. You can use ITypedList
to return virtual PropertyDescriptor
whose GetValue
and SetValue
methods use the array
For anybody who is trying to implement this, declare your class as an expandoobject this a flexible class structure with implements INotifyPropertyChanged and you can define the properties as required in code.
On the telerik raddatagrid bind the itemsource to a collection of the expandoobjects and set AutoGenerateColumns to true.
If you want to tailor the columns based upon the contents of the expandoobject then use the AutoGeneratingColumn event, this is fired for each column generate, if you define your column controls as datatemplates you can access them in the code behind and assign them too the cell template.
Not sure about RadGridView
but for a normal DataGrid
you can either set the AutoGenerateColumns
Property to true
to auto generate columns based on its data binding or false
to create your columns based on your code.
In your case, I think you have to set AutoGenerateColumns
Property to false
and define your columns by yourself to combine both (elements of your collection and the Code property).
EDIT:
Just checked the MSDN :
Explicitly declared column fields can be used in combination with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields.
精彩评论