OK... I have kindof a sepecialised case. I am getting log data back from a DB that has 5 static properties and then any number of variable properties. The class looks like this:
Prop 1, prop 2, prop 3, collection of properties 4-n... all possible properties are known.
Based on the type of log, properties 4-n will be different, again, all possible properties will be known.
The values of these properties will always be text.
binding properties 1-3 are easy... but is it possible to bind column values to items within a sub collection? for example, key="{Binding PropCollection.Prop4}"
Thanks in advance for any help
Marc
Hi Marc,
If all properties are known, couldn't you create an object that contains properties for all possible properties being sent to the client?
Then when you get the data, loop through the properties and create columns for the props you got back?
If thats not possible, you can explore the options suggested in the following post, using TemplateColumns with DataTemplates built from strings, or creating a custom Column.
http://community.infragistics.com/forums/p/32185/175645.aspx#175645
-SteveZ
Yes... but that would be the easy way out and we wouldnt be programming Micrrosoft products if we settled for that... ;)
no, actually, I have about a dozen types of logs that contain the same core data, but each log has different sets of property data that would be within the collection. I would like to avoid defining on specific class for each of the log types. What I would like to do is have a generic class that contains the core data and then a collection of log entry proprties for the variable stuff. The collection would contain the property name, ID and value. When I get my log back I want to create the columns for that log type dynamically, then as I'm populating my rows, assign them to the specific properties within the sub collection.
Then i'd recommend using a Custom Column.
Here is some code to get you started:
Step 1: Create a Custom Column and ColumnContentProvider:
public class MyColumn : TemplateColumn
{
protected override ColumnContentProviderBase GenerateContentProvider()
return new MyColumnContentProvider();
}
public class MyColumnContentProvider : TemplateColumnContentProvider
public override FrameworkElement ResolveDisplayElement(Cell cell, Binding cellBinding)
FrameworkElement elem = base.ResolveDisplayElement(cell, cellBinding);
if (elem != null)
MyDataObject mdo = cell.Row.Data as MyDataObject;
if(mdo != null)
elem.DataContext = mdo.Properties[cell.Column.Key];
return elem;
Step 2: Create a generic DataTemplate:
<DataTemplate x:Key="colTemplate">
<TextBlock Text="{Binding}"></TextBlock>
</DataTemplate>
Step 3: Generate your Columns:
DataTemplate template = this.Resources["colTemplate"] as DataTemplate;
this.DataGrid1.Columns.Add(new MyColumn() { ItemTemplate = template, Key = "Prop1" });
this.DataGrid1.Columns.Add(new MyColumn() { ItemTemplate = template, Key = "Prop2" });
this.DataGrid1.Columns.Add(new MyColumn() { ItemTemplate = template, Key = "Prop3" });
What i'm doing:
Basically, i create a custom column, with a custom content provider, that changes the DataContext of the binding of the my DataTemplate. The data i'm creating is a Dictionary, which i'm assuming you're using as well.
My DataTemplate, just creates a straight binding, with no property path, b/c the datacontext i'm binding to is the value that i want to display.
Then i just walk through and generate my columns. Note, i'm using my column key to determine the properties from the Dictionary. But since this is your custom column, you can add additional properties and customize it to your wishes.
Hope this helps,
I had the same problem, and this approach seamed to work great. one issue is now if i drag a column into the group by area ir gives the follow error:
Unhandled Error in Silverlight Application Object reference not set to an instance of an object. at Infragistics.Silverlight.DataManagerBase.ResolvePropertyTypeFromPropertyName(String propertyName, Type rootType)\n at Infragistics.Silverlight.GroupByContext.CreateGenericCustomGroup(Type type, String propertyName, Object comparer)\n at Infragistics.Silverlight.RowsManager.InvalidateGroupBy(Boolean reset)\n at Infragistics.Silverlight.XamWebGridRowsManager.InvalidateGroupBy(Boolean reset)\n at Infragistics.Silverlight.RowsManager.OnColumnLayoutPropertyChanged(ColumnLayout layout, String propertyName)\n at Infragistics.Silverlight.XamWebGridRowsManager.OnColumnLayoutPropertyChanged(ColumnLayout layout, String propertyName)\n at Infragistics.Silverlight.RowsManagerBase.a(Object A_0, PropertyChangedEventArgs A_1)\n at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)\n at Infragistics.Silverlight.DependencyObjectNotifier.OnPropertyChanged(String name)\n at Infragistics.Silverlight.Controls.ColumnLayout.InvalidateGroupBy()\n at Infragistics.Silverlight.Controls.GroupByColumnsCollection.OnGroupingChanged(IList`1 oldCollection, IList`1 newCollection)\n at Infragistics.Silverlight.Controls.GroupByColumnsCollection.AddItem(Int32 index, Column item)\n at Infragistics.Silverlight.CollectionBase`1.Add(T item)\n at Infragistics.Silverlight.Controls.Column.set_IsGroupBy(Boolean value)\n at Infragistics.Silverlight.Controls.Primitives.HeaderCellControl.OnMouseLeftButtonUpColumnMoving(MouseButtonEventArgs e)\n at Infragistics.Silverlight.Controls.Primitives.CellControlBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)\n at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)\n at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
any ideas?
That is because currently functions such as sorting, grouping, and filtering do not work on template columns.
So what are my options? I essentially have a IDictionary that is my main datasource that gets returned by my SOA layer for all calls to my application server. The dictionary object is also used heavily in all our clients including our WPF based clients where binding with a index works ( Text="{ Binding datasourceprops [fieled] }" ) so i cannot duplicate data objects on the client, im stuck with the the IDcitionary
in silverlight I wrote a converter for all fileds that pull out the correct values from the dictionary, what can i do in the Infragistic grid? any ideas?
thanks
Well I am confused on your problem. If your problem is creating the columns then many approaches were posted previously, including the one I suggested here.
If your problem is grouping and sorting, well that is just a limitation of TemplateColumns in the XamDataGrid for now, and you may want to try Stephen's suggestion of trying the unboundColumn in the 10.1 CTP.
I understand your situation. I am sure you will find other things that you will not like in Silverlight as well, such as dealing the the ComboBox, or trying to Print or export data to Excel (luckily we get the base funcitonality of exporting with Infratgistics.Exel, but it is limited). For now, I would try to UnboundColumn approach and see if it will meet your needs. Let me know if it works for you, because I am in the same boat as your are. I want my sorting, and grouping on TemplateColumns!
Oh just a heads up, if you are planning on exporting your data to Excel using Infagistics.Excel, be aware that when you use TemplateColumns, the Cell.Value will be null, meaning that you cannot grab the value from the XamWebGrid. You will have to get the value from your underlying data source instead.
Sorry fro the confusion. originally I could not get the data to display, your suggestion and example fixed that. then i ran into the grouping and sorting limitations of the approach.
SO i want my cake and eat it to. i wan to display the data and be able to group and sort it bacause we allow our customers to do this in our WPF client and they would not like it if i took it away in our new shiny SL client.