代码之家  ›  专栏  ›  技术社区  ›  thmsn

WPF半级联列表框

  •  1
  • thmsn  · 技术社区  · 15 年前

    我的页面上有3个控件

    • 列表框A
    • 列表框B

    列表框A被数据绑定到项A的集合

    列表框B被数据绑定到项B的集合

    B引用了项a和项C,列表框B只应显示项a是列表框a的选定项,项C是列表框C的选定项的项

    我把collectionviews作为ListBox B上的ItemSource设置了一个过滤器,但我只能让它基于ListBox a或ComboBox C更新ListBox B的内容,而不是同时更新两者。

    有什么想法吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Robert Rossney    15 年前

    在您的视图模型中,给出 CollectionView 一个过滤器谓词,如下所示:

    Items = CollectionViewSource.GetDefaultView(_Items) as CollectionView;
    Items.Filter = (x => ((Item)x).CategoryA == SelectedCategoryA 
                    && ((Item)x).CategoryC == SelectedCategoryC);
    

    绑定列表/组合框' SelectedItem SelectedCategoryA SelectedCategoryC 财产。在这些属性的setter中,调用 Items.Refresh() .

    在列表框中,将两者绑定 ItemsSource ,例如。

    <ListBox ItemsSource="{Binding CategoryListA}"
             SelectedItem="{Binding SelectedCategoryA, Mode=TwoWay}"/>
    

    private Category _SelectedCategoryA;
    
    public Category SelectedCategoryA
    {
       get { return _SelectedCategoryA; }
       set
       {
          if (value != _SelectedCategoryA)
          {
             _SelectedCategoryA = value;
             Items.Refresh();
          }
       }
    }
    
        2
  •  0
  •   Val    15 年前

    一种解决方案是从B类型的公共访问器属性集合中创建一个独立的B项集合。

    private List<B> m_trueCollection; //the actual collection of B
    public ObservableCollection<B> FilteredB { get; set; } //bind to this
    

    然后,每当组合框C或列表框a的选定项发生更改时,请收听 SelectionChanged 事件属性。

    如果更改了选择,则迭代true集合并根据条件重新生成FilteredB。

    推荐文章