WPF层次树视图多重绑定混合列表问题

发布于 2024-09-27 14:28:54 字数 588 浏览 2 评论 0原文

我很难解决这个简单的问题:

我使用带有 HierarichalDataTemplate 的树视图来显示两个实体的层次结构:OrganizationUnit。该组织与其自身具有多对多的关系,因为它可能有子组织。每个组织与单元具有一对多的关系,因为一个组织可以包含多个单元。

我成功地使用带有可观察集合的多重绑定来填充树。转换器在初始化时被调用,但我无法再次调用它。因此,当我添加任何单位或组织时,列表不会更新:

  <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-10-04 14:28:54

我认为这里发生的情况是,当您将集合传递给转换器(即“TreeMultiValueConverter”)时,您将这两个集合合并为一个集合,例如 ObservableCollection 并将其作为源返回。

这正是当您向单位或组织集合添加新项目时,集合更改事件不会传播到 HierarchicalDataTemplate 的 ItemsSource 的原因。

解决方案:

在转换器中返回列表集合视图,并更新集合更改后的视图。

public object Convert(object[] values, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
{
    if(values == null || values.Length != 2) return null;

    var combinedList = new List<object>();
    var listCollectionView= new ListCollectionView(combinedList);    

    var childOrgs = values[0] as ObservableCollection<Organization>;
    if(childOrgs != null)
    {
        combinedList.AddRange(childOrgs);
        childOrgs.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }
    var units = values[1] as ObservableCollection<Unit>;
    if(units != null)
    {
        combinedList.AddRange(units);
        units.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }

    listCollectionView.Refresh();
    return listCollectionView;
}

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.

public object Convert(object[] values, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
{
    if(values == null || values.Length != 2) return null;

    var combinedList = new List<object>();
    var listCollectionView= new ListCollectionView(combinedList);    

    var childOrgs = values[0] as ObservableCollection<Organization>;
    if(childOrgs != null)
    {
        combinedList.AddRange(childOrgs);
        childOrgs.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }
    var units = values[1] as ObservableCollection<Unit>;
    if(units != null)
    {
        combinedList.AddRange(units);
        units.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }

    listCollectionView.Refresh();
    return listCollectionView;
}
一念一轮回 2024-10-04 14:28:54

我已经使用实体类的部分扩展解决了我的问题,并添加了一个同时包含组织和单位类型的“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文