Hi IGTeam,
I am in a need to dynamically create columns for my igGrid;
I bind to data from my ViewModel, which is of type ObservableCollection<DynamicObject>;
Just before the binding, I want to create columns on the View side like this:
1. Create collection of TemplateColumn
2. Send to View
3. Simply add columns to grid
____________________________________________________________________
1. Iteration in for block (populate _dynamicGridColumnKeys)
TemplateColumn column = new TemplateColumn(); column.HeaderText = presentingName; column.ItemTemplate = (System.Windows.DataTemplate)System.Windows.Markup.XamlReader.Load(CreateColumnTemplate(propertyName)); //display template
private string CreateColumnTemplate(string propertyName) { StringBuilder CellTemp = new StringBuilder(); CellTemp.Append("<DataTemplate "); CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/"); CellTemp.Append("2006/xaml/presentation' "); CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>"); CellTemp.AppendFormat("<TextBlock Text='{{Binding [{0}]}}'/>", propertyName); CellTemp.Append("</DataTemplate>"); return CellTemp.ToString(); }
2.
Messenger.Default.Send<List<TemplateColumn>>(_dynamicGridColumnKeys, Preferences.CurrentGridName);
3.
Messenger.Default.Register<List<TemplateColumn>>(this, GridName, p => { foreach (var item in p) { this.customGrid.Columns.Add(item); } });
Can you please tell me why this works in System.Windows.Controls.DataGrid and I can't seem to fix it with the Infragistics one?
Please, this is important for me, I would appreciate if you inform me about this DynamicObject issue soon
Thanks
Hi,
Silverlight doesn't support binding to Dynamic objects:
http://connect.microsoft.com/VisualStudio/feedback/details/522119/databinding-to-dynamic-objects-is-broken
http://connect.microsoft.com/VisualStudio/feedback/details/524678/silverlight-4-data-binding-does-not-work-with-dynamicobject
Which is why the xamGrid isn't resolving the data.
-SteveZ
Hi Stephen,
Thanks for the reply;
Please note that I am using DynamicObject as a datasource for the grid;
Hence, I know the property names when in runtime !
I tried to set the Key property of TemplateColumn, with no success nor exception - no columns;
(tried both with [PropertyName] and PropertyName)
AFAIK, common notation for binding to DynamicObject is : {Binding [DynamicProperty]}
My question:
Do you support DynamicObject binding?
If yes, please revisit my question again and tell me how would I set the Key property, or maybe I even have a mistake in the DataTemplate part (which works with silverlight's grid)...
All columns int he xamGrid require their Key property to be set. If your column is bound to your data source, then you can use a TemplateColumn where the key corresponds to a property in your underlying datasource. If not, you can use an UnboundColumn and use any unique key.
You should be getting an exception that describes this, when you attempt to set add a column with an Invalid key or no key at all.