简体   繁体   中英

Auto generate textboxes for properties with public set C# WPF

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

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 (ie 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

我用这个来解决我的问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM