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,
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.
-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)...
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.
Yes sorting and groupBy work out of the box with property indexers.
Filtering and summaries will work, however the property indexer needs to be typed. For example, your Dictionary should be someting like Dictionary<string, Foo>, not Dictionary<string, Object>
http://help.infragistics.com/NetAdvantage/Silverlight/2011/1/CLR4.0/?page=xamGrid_Support_for_String_Indexers.html
Will sorting be supported with property indexers in the 10.3 release of the xamgrid?
Yes, that is exactly what I meant!
Since I am using 10.1 still, this is valuable information for me
Yes, it doesn't support DynamicObject binding, which means it won't resolve properties directly on your object.
You're talking about a workaround using indexer bindings. Which the xamGrid does support, as of the 10.3 release.
List<DynamicDataContext> data = new List<DynamicDataContext>();
for(int i = 0; i < 100; i++)
{
DynamicDataContext ddc = new DynamicDataContext();
ddc["Test"] = i;
data .Add(ddc);
}
grid.ItemsSource = data;
<ig:XamGrid.Columns>
<ig:TemplateColumn Key="[Test]">
<ig:TemplateColumn.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding [Test]}"/>
</DataTemplate>
</ig:TemplateColumn.ItemTemplate>
</ig:TemplateColumn>
</ig:XamGrid.Columns>
Please don't mind me asking, but are you sure about that?
http://www.xavierdecoster.com/post/2010/10/08/Silverlight-4-Data-Binding-to-Dynamic-DataContext.aspx
This is more or less the way I got this 'unsupported' binding to work with my silverlight datagrid;
But anyways, that means that igGrid doesn't have this functionality, right?!