2010-10-31 29 views
1

我有我的网页上3个控制WPF半级联列表框

  • 列表框一个
  • ListBox中乙
  • 组合框Ç

ListBox中A是数据绑定到的项目的

集合

组合框C是数据绑定到项目集合C

列表框B是数据绑定到项目集合B

B对项目A和项目C有引用,列表框B应该只显示项目A是ListBox A的选定项目,项目C是选择的项目列表框C

我已经弄乱了一点ListBox B上的ItemSource的collectionviews,设置一个过滤器,但我只能得到它来更新基于ListBox A或ComboBox C的ListBox B的内容,而不是在同一时间。

任何想法?

回答

2

在您的视图模型,给CollectionView一个过滤谓词,这样的事情:

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

绑定列表/组合框SelectedItemSelectedCategoryASelectedCategoryC性能。在这些房产的筹码人中,请致电Items.Refresh()

编辑

在你的列表框,都绑定和ItemsSourceSelectedItem,例如

<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(); 
     } 
    } 
} 
+0

你能如何将selectedItem属性绑定和调用items.refresh详细点吗? – thmsn 2010-11-01 12:42:41

+0

当然;看我的编辑。 – 2010-11-01 15:13:54

0

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

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

然后,收听到每当有使用SelectionChanged事件属性组合框C或列表框A的所选项目的变化。

如果更改了选择,请按照您的条件迭代真实集合并重建FilteredB。

希望它有帮助。