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,
Thanks... I'll give that a try.
Thanks a lot, that seems to have worked well.... However I do have one other question. Is it possible to SORT on those custom columns that you have just created?
Thanks
Currently, no, you won't have sorting, filtering or groupBy support for those columns, as they require a property on the data object, as we use linq to perform all of those operations.
However, this is something that we are actively looking into enabling.