使用MVVM在WPF中进行COMBOBOX过滤

red*_*rus 3 wpf combobox mvvm itemssource

我正在使用WPF mvvm方法开发一个应用程序.我有一个要求,我必须在组合框中显示项目列表供选择.基于某些标志,我需要从组合框中过滤掉一些项目以供选择.

我尝试使用两个不同的项目源,一个带有完整列表,另一个带有过滤列表,并根据标志我想要更改项目源.这似乎不太好用.有没有简单的方法根据某些标志在现有列表上应用过滤器?

Mar*_*een 5

有很多不同的方法可以做到这一点,但我个人的偏好是使用a ListCollectionView作为显示过滤列表的控件的ItemsSource,设置过滤器谓词,ListCollectionView.FilterListCollectionView.Refresh在过滤器参数更改时调用.

以下示例将根据其大陆过滤国家/地区列表.

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;

public class FilteringViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Country> _countries;
    private ContinentViewModel _selectedContinent;

    public ListCollectionView CountryView { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<ContinentViewModel> Continents { get; set; } 

    public FilteringViewModel()
    {
        _countries =
            new ObservableCollection<Country>(
                new[]
                    {
                        new Country() { Continent = Continent.Africa, DisplayName = "Zimbabwe" },
                        new Country() { Continent = Continent.Africa, DisplayName = "Egypt" },
                        new Country() { Continent = Continent.Europe, DisplayName = "United Kingdom" }
                    });
        CountryView = new ListCollectionView(_countries);
        CountryView.Filter = o => _selectedContinent == null || ((Country)o).Continent == _selectedContinent.Model;

        Continents = new ObservableCollection<ContinentViewModel>(Enum.GetValues(typeof(Continent)).Cast<Continent>().Select(c => new ContinentViewModel { Model = c}));
    }

    public ContinentViewModel SelectedContinent
    {
        get
        {
            return _selectedContinent;
        }
        set
        {
            _selectedContinent = value;
            OnContinentChanged();
            this.OnPropertyChanged("SelectedContinent");
        }
    }

    private void OnContinentChanged()
    {
        CountryView.Refresh();
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Country
{
    public string DisplayName { get; set; }
    public Continent Continent { get; set; }
}

public enum Continent
{
    [Description("Africa")]
    Africa,
    Asia,
    Europe,
    America
}

public class ContinentViewModel
{
    public Continent Model { get; set; }
    public string DisplayName
    {
        get
        {
            return Enum.GetName(typeof(Continent), Model);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML

<StackPanel Orientation="Vertical">
    <ComboBox ItemsSource="{Binding Continents}" SelectedItem="{Binding SelectedContinent}" DisplayMemberPath="DisplayName" />
    <ListBox ItemsSource="{Binding CountryView}" DisplayMemberPath="DisplayName" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)