wpf - 带有 MVVM 的 WPF 中的 COMBOBOX 过滤

标签 wpf mvvm combobox itemssource

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

我尝试使用两个不同的项目来源,一个是完整列表,另一个是过滤列表,并根据我想更改项目来源的标志。 这似乎效果不佳。是否有任何简单的方法可以根据某些标志在现有列表上应用过滤器?

最佳答案

有很多不同的方法可以做到这一点,但我个人的偏好是使用 ListCollectionView 作为显示过滤列表的控件的 ItemsSource,在 ListCollectionView 上设置过滤谓词。过滤器并在过滤器参数改变时调用ListCollectionView.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);
        }
    }
}

XAML

<StackPanel Orientation="Vertical">
    <ComboBox ItemsSource="{Binding Continents}" SelectedItem="{Binding SelectedContinent}" DisplayMemberPath="DisplayName" />
    <ListBox ItemsSource="{Binding CountryView}" DisplayMemberPath="DisplayName" />
</StackPanel>

关于wpf - 带有 MVVM 的 WPF 中的 COMBOBOX 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554569/

相关文章:

HTML "for"属性的 WPF 标签对应物

c# - 在 WPF 中鼠标悬停时如何更改组合框边框的颜色?

java - 如何使选定的组合框值显示不同的文本?

C# - 如何从组合框中检索对对象的引用?

c# - WPF:数据绑定(bind)对于模态对话框是否重要?

wpf - 使用Xceed PropertyGrid

wpf - WPF 中两个 ListBox 之间的通信

c# - 使用viewmodel在wpf窗口中实现进度条

c# - 使用 DoubleAnimation 设置图像不透明度

c# - ComboBox SelectedValue 或 SelectedItem 绑定(bind) WPF C#