Wpf treeview selectedItem databinding

南楼画角 提交于 2019-12-25 03:53:16

问题


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 them 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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6308817/wpf-treeview-selecteditem-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!