Sign in to follow this  

[.net] WPF data binding without existing types with properties

This topic is 3279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to allow users to edit data via a DataGrid. The type of data shown in that DataGrid varies; users can add, remove, and edit the fields on the database externally. Currently I'm working out the type of each column and using the TypeBuilder to create a new type at runtime with all the requisite properties in place. I then use reflection to populate each instance with the correct values before displaying them in the DataGrid. Is there a better way of doing this?

Share this post


Link to post
Share on other sites
You can use a property indexed by a string:

class DataTypeWrapper
{
public object this[string name]
{
get
{
//return the value for property "name" from the database
}
}
}

You can databind to the indexer in xaml:

<TextBlock Text="{Binding DataTypeWrapper[Baz]}"/>

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
Whoops, I tried that but didn't include the type name (so just {Binding [Baz]}). Thanks again!


It's not that you need the type name per-se - you just need a property to get to the indexer. In my code i'm assuming your data context is something with a property named "DataTypeWrapper"

Share this post


Link to post
Share on other sites
I can't get it to work, I'm afraid. I'm trying this:

this.dataGrid.Columns.Add(new DataGridTextColumn() {
Binding = new Binding("[some_key]"),
});


The Binding constructor throws an exception: "Invalid Binding Path; character [ in [some_key]".

Edit: Same happens with this:

var TempColumn = @"<data:DataGridTextColumn xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:data='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data' Header=""???"" Binding=""{Binding [some_key]}"" />";
this.dataGrid.Columns.Add((DataGridTextColumn)XamlReader.Load(TempColumn));

Could it be that this is something that doesn't work in Silverlight (but does in regular WPF?)

[Edited by - benryves on December 19, 2008 7:03:55 AM]

Share this post


Link to post
Share on other sites

This topic is 3279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this