ICollectionView-筛选并显示组中的所有项目

时间:2019-01-15 15:40:52

标签: c# wpf collectionview

当组中的一个项目与过滤器匹配时(如何以一种高效的方式),如何过滤项目但显示组中的所有项目?

这是我的代码,我搜索“ John”,并希望看到John和Sammy(同一组=男性),但不想看到Jane。目前只显示约翰。

public partial class FilteringSample : Window
    {
        public FilteringSample()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Sex = SexType.Male });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Sex = SexType.Female });
            items.Add(new User() { Name = "Sammy Doe", Age = 13, Sex = SexType.Male });
            lvUsers.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Sex");
            view.GroupDescriptions.Add(groupDescription);           
            view.Filter = UserFilter;
        }

        private bool UserFilter(object item)
        {
            if(String.IsNullOrEmpty(txtFilter.Text))
                return true;
            else
                return ((item as User).Name.IndexOf(txtFilter.Text, StringComparison.OrdinalIgnoreCase) >= 0);
        }

        private void txtFilter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            CollectionViewSource.GetDefaultView(lvUsers.ItemsSource).Refresh();
        }
    }

    public enum SexType { Male, Female };

    public class User
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Mail { get; set; }

        public SexType Sex { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

这个怎么样? “性能”取决于您要处理的集合的大小以及所需的性能。

private List<SexType> _currentFilters = new List<SexType>();

public FilteringSample()
{
    InitializeComponent();

    List<User> items = new List<User>();
    items.Add(new User() { Name = "John Doe", Age = 42, Sex = SexType.Male });
    items.Add(new User() { Name = "Jane Doe", Age = 39, Sex = SexType.Female });
    items.Add(new User() { Name = "Sammy Doe", Age = 13, Sex = SexType.Male });
    lvUsers.ItemsSource = items;

    UpdateFilters();

    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("Sex");
    view.GroupDescriptions.Add(groupDescription);
    view.Filter = UserFilter;
}

private bool UserFilter(object item)
{
    return _currentFilters.Contains(((User)item).Sex);
}

private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
{
    UpdateFilters();
    CollectionViewSource.GetDefaultView(lvUsers.ItemsSource).Refresh();
}

private void UpdateFilters()
{
    //Select the SexType(s) of the Users matching the current txtFilter name filter, or of all Users if txtFilter is empty
    string searchString = txtFilter.Text;
    IEnumerable<User> users = lvUsers.ItemsSource as IEnumerable<User>;

    if(!String.IsNullOrWhiteSpace(searchString))
        users = users.Where(u => u.Name.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0);

    _currentFilters = users.Select(u => u.Sex)
                            .Distinct()
                            .ToList();
}