开发者

Wpf treeview selectedItem databinding

开发者 https://www.devze.com 2023-03-12 05:43 出处:网络
I have a view where I\'ve got an object bound to a treeview. The object has a number of collections (of different types) so I\'m using hiearchical templates with a CompositeCollection to display t开发

I have a view where I've got an object bound to a treeview. The object has a number of collections (of different types) so I'm using hiearchical templates with a CompositeCollection to display t开发者_运维百科hem in the treeview.

I've then got a textbox that is bound to the treeview's selectedItem. Here I'm serialising the selectedItem to XML and displaying it in the textbox for editing.

All good so far. However, the big problem I have is that I can't use 2-way databinding with the SelectedItem property of the treeview as it is read only.

How can I cleanly keep the textbox edits in sync with my object that is bound to the treeview?


I do not think you need to do two-way databinding on the SelectedItem itself, you should expose a property in the class of your bound object which returns the serialized string and upon set modifies the object appropriately. This should be easier than dealing with the object as a whole.


Your XML stream must be represented as a property on your SelectedItem node, and your TextBox must be bound to that, somehow. The SelectedItem is read-only, but the object it refers to is not. If you two-way bind on that property, you should be able to affect your edits correctly. This would be done in the DataTemplates and HiearchicalDataTemplates you are using, since they are bound to the underlying data representation of the nodes you are representing with the TreeView.

0

精彩评论

暂无评论...
验证码 换一张
取 消