0

I want to be able to bind a listbox, Treeview or some control to a ObservableCollaction>SomeObject>() and then auto generate textboxes to all properties with a public set within SomeObject. That way I dont have to add or remove any code in WPF if someone remove or add a new property to SomeObject.

Can anyone please point me in the right direction or tell me if this is a stupid idea?

//Max

Anderung
  • 31
  • 3

2 Answers2

2

I don't think you should use ObservableCollection for that. That's used when you want to present a collection that will be modified in runtime (i.e. having elements added, removed or modified). Do you expect SomeObject properties names to be modified at runtime?

If you just want to expose the names of the properties and their values, use Reflection. Take a look at this. Add some behind code to create a collection of pairs with names and values. That should do the trick

Then use some DataTemplate to create the textboxes from this info. I find this tutorial very useful

Community
  • 1
  • 1
Diego O.d.L
  • 183
  • 9
  • I manage to solve my problem with help from [this](https://rbrundritt.wordpress.com/2012/01/30/view-object-properties-in-wpf-treeview/). I added a CanWrite property to ObjectNode and then I used DataTemplateSelector to separate the properties with private set from the ones with public set. Then i subscribe to propertychanged on the NodeObject to convert the changed values back to the original object. – Anderung Jul 07 '15 at 07:17
0

I used this to solve my problem

Anderung
  • 31
  • 3