MVVM - 制作ObservableCollection过滤器

时间:2015-02-18 17:03:40

标签: c# wpf xaml mvvm

我正在尝试创建一个可以过滤的列表。 首先在我的xaml。

<CollectionViewSource x:Key="ExpectedGroup"                               
          Source="{Binding ExpectedListView, UpdateSourceTrigger=PropertyChanged}">
          <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="InductedSites" />                           
          </CollectionViewSource.GroupDescriptions>
           <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="InductedSites"/>
            <scm:SortDescription PropertyName="PN_EmploymentType"/>
            <scm:SortDescription PropertyName="FullName"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

并在我的viewmodel中

private ICollectionView _expectedListView;
    public ICollectionView ExpectedListView
    {
        get { return _expectedListView; }
        set { _expectedListView = value; RaisePropertyChanged("ExpectedListView"); }
    }

private ObservableCollection<BoonPerson> _expectedList;
    public ObservableCollection<BoonPerson> ExpectedList
    {
        get { return _expectedList; }
        set { _expectedList = value; RaisePropertyChanged("ExpectedList"); }
    }

我首先填充我的observablecollection,然后将数据移动到ICollectionView

ExpectedList = await _clockInService.LoadExpectedList();
ExpectedListView = CollectionViewSource.GetDefaultView(ExpectedList);

**编辑 和列表框

<ListBox Grid.Row="2" x:Name="lbExpected" 
 DataContext="{StaticResource ExpectedGroup}"
 Background="{x:Null}" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True"
                                Margin="0,2,0,0" FontSize="10.667">

但列表始终为空。 我通过制作我的列表框itemsource = ExpectedListView证明了这个过程是有效的。 我到处搜寻,找不到我做错了什么。 感谢任何指针。斯科特

1 个答案:

答案 0 :(得分:0)

你想:

<ListBox ItemSource="{Binding Source={StaticResource ExpectedGroup}}" .../>

Not DataContext =&#34; {StaticResource ExpectedGroup}&#34;。