问题描述
我有一个视图,其中有一个绑定到树视图的对象。该对象具有多个(不同类型的)集合,所以我使用带有CompositeCollection的分层模板在树状视图中显示它们。
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.
然后,我得到了一个绑定到树视图的selectedItem的文本框。在这里,我将selectedItem序列化为XML,并将其显示在文本框中进行编辑。
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.
到目前为止,一切都很好。但是,我遇到的最大问题是,我无法对树视图的SelectedItem属性使用2向数据绑定,因为它是只读的。
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?
推荐答案
我认为您不需要做两个-在SelectedItem本身上进行双向数据绑定时,应在绑定对象的类中公开一个属性,该属性返回序列化的字符串,并在 set
进行适当修改后。这比处理整个对象要容易。
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.
这篇关于WPF TreeView selectedItem数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!