WPF层次树视图多重绑定混合列表问题
我很难解决这个简单的问题:
我使用带有 HierarichalDataTemplate 的树视图来显示两个实体的层次结构:Organization
和 Unit
。该组织与其自身具有多对多的关系,因为它可能有子组织。每个组织与单元具有一对多的关系,因为一个组织可以包含多个单元。
我成功地使用带有可观察集合的多重绑定来填充树。转换器在初始化时被调用,但我无法再次调用它。因此,当我添加任何单位或组织时,列表不会更新:
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource TreeMultiValueConverter}">
<Binding Path="ChildOrgs"/>
<Binding Path="Units" />
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
请帮忙... 谢谢
I'm having a tough time solving this simple issue :
I am using a treeview with HierarichalDataTemplate to show an hierarchy of two entities : Organization
and Unit
. The Organization has a many to many relation with itself as it could have sub organizations. And each Organization has one to many relation with Unit, as an organization could contain several units.
I am successfully using multibindings with observable collection to populate the tree. The converter is called in initialization, but i cant get it to be called again. So when i add any units or organizations, the list is not updated:
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource TreeMultiValueConverter}">
<Binding Path="ChildOrgs"/>
<Binding Path="Units" />
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
Please help...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这里发生的情况是,当您将集合传递给转换器(即“TreeMultiValueConverter”)时,您将这两个集合合并为一个集合,例如 ObservableCollection 并将其作为源返回。
这正是当您向单位或组织集合添加新项目时,集合更改事件不会传播到 HierarchicalDataTemplate 的 ItemsSource 的原因。
解决方案:
在转换器中返回列表集合视图,并更新集合更改后的视图。
I think what's happening here is when you pass your collections to the converter (i.e. "TreeMultiValueConverter") you are merging the two into one collection, say a ObservableCollection and returning it as the source.
This is precisely why when you add a new item to your unit or organization collection, the collection changed event is not propagated to the ItemsSource of the HierarchicalDataTemplate.
Solution:
Return a list collection view in you converter and update the view on collection changed.
我已经使用实体类的部分扩展解决了我的问题,并添加了一个同时包含组织和单位类型的“MixedChilds”集合。
当 HierarchialDataTemplate 绑定到它时,一切正常。
I have solved my problem using partial extending of my entity class, and adding a 'MixedChilds' collection which contains both Organization and Unit types together.
When the HierarchialDataTemplate is bound to that, everything works.