views:

25

answers:

1

I've got user-definable columns for my data grid, which basically boils down to a

switch(column_title)
{
case "foo": binding = new Binding("Model.Fonz");
//etc.
}

and the binding gets applied to the column.

Now I need to dump to csv, with the configured columns. As it may be a different column set that being used in my ui and I definitely don't want two huge switch statements to maintain, I'd essentially like a function like this:

object GetBoundProperty(object o, System.Windows.Data.Binding binding)

I won't be surprised if its obnoxiously easy, but its outside the range of my .NET knowledge at the moment and I have little desire to parse the periods out of the binding and search through reflection unless I totally have to. Thanks!

+1  A: 

Hi Tom,

Let me suggest something else. I just don't like idea with getting data from model through databindings when you can get it directly from model... Rather than having two big switches you could create one class per column, that will handle your data requests. In pseudocode it will look like this:

ColumnData fonzColumnData = ColumnsFactory.Create(columnTitle);

// for bindings:
Binding binding = fonzColumnData.Binding;
// for CSV:
string csvData = fonzColumn.CSVData;
// ...
Anvaka
Thanks for the suggestion, probably a wise route. Biggest hurdle is I use the same factory for two different (very similar) models/viewmodels and the factory doesn't care since Binding just takes a string.I guess ColumnData.CSVData could be a func<string, object> and it's probably a good time to make an Interface for the two model's to share.Not the low-code solution I was hoping for but will probably improve my app.
Tom