【问题标题】:COMBOBOX filtering in WPF with MVVM带有 MVVM 的 WPF 中的 COMBOBOX 过滤
【发布时间】:2014-02-28 13:05:10
【问题描述】:

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

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

【问题讨论】:

  • 什么不起作用?您还需要添加更多细节和代码,这不起作用。

标签: wpf mvvm combobox itemssource


【解决方案1】:

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

【讨论】:

  • 谢谢。这种方法对我有用。非常感谢您节省了我几个小时的时间。欣赏它。
【解决方案2】:

是否有任何简单的方法可以根据现有列表应用过滤器 一些标志?

虽然你的问题不清楚,但我认为你不需要维护两个列表来获取过滤数据。您可以使用简单的 LINQ 进行过滤。假设你有一个 ViewModel 属性,比如

public IEnumerable<ComboBoxItem> Data
    {
        get ;
        set ;
    }

如果你想根据一些布尔值过滤它,那么你可以写类似

ViewModel.Data.ToList().Where(item => item.Status).ToList()

状态可以是基于您要过滤数据的布尔值,您可以在 ComboBoxItem 类中添加此布尔值。

【讨论】:

  • 但是如何将结果关联为组合的ItemSource
猜你喜欢
  • 2012-12-31
  • 2012-06-19
  • 2011-08-23
  • 2017-02-20
  • 2015-03-29
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 2012-05-04
相关资源
最近更新 更多