如何基于CheckBox值(MVVM)筛选集合

时间:2020-03-24 18:34:54

标签: c# wpf checkbox mvvm

我有一个员工表。我有一个WPF屏幕。在屏幕上,我希望有一个“排除前雇员”复选框(默认选中),当它选中“我生成报告”时,表中没有前雇员。但是当我取消选中复选框时,我想看到以前的雇员。

在我的xaml中查看:

<CheckBox Content="Exclude Former Employees" IsChecked="{Binding ExcludeFormerEmployees}" Margin="4" />

在ViewModel中:

我的生成报告按钮命令(以记录员工报告。)

private void GenerateReports()
        {
            IsBusy = true;
            var harmonyDatas = ConvertRawDatas(SelectedYear, SelectedMonths, SelectedEmployees);

            harmonyDatas.ForEach(hd =>
            {
                if (_excludeWeekends) hd.ExcludeWeekends();
                if (_excludePublicHolidays) hd.Exclude(_publicHolidays);
                if (_excludeFormerEmployees) hd.ExcludeFormerEmployees();
            });

            ParticularEntries = harmonyDatas.SelectMany(hd => hd.Select(range => new EntryReportParticular
            {
                Employee = range.Employee,
                Entry = range.Start,
                Exit = range.End,
                Region = range.Region
            }));

            DailyEntries = ParticularEntries.GroupBy(p => p.Employee.Id).SelectMany(pe => pe.GroupBy(peg => peg.Entry.Date).Select(peg =>
            {
                var firstPe = peg.First();

                return new EntryReportDaily
                {
                    Employee = firstPe.Employee,
                    Day = firstPe.Entry.Date,
                    TotalWorkingHours = peg.Sum(entry => entry.Duration.TotalHours)
                };
            }));

            MonthlyEntries = MonthlyEntries = harmonyDatas.Select(hd => new EntryReportMonthly
            {
                Employee = hd.Employee,
                NofWorkingDaysAtOrigLoc = hd.GetNofDaysInMonthAtOrigLoc(),
                NofWorkingDaysAtOtherLoc = hd.GetNofDaysInMonthAtOtherLoc(),
                TotalWorkingHoursAtOrigLoc = hd.GetMonthlyWorkingHoursAtOrigLoc(),
                TotalWorkingHoursAtOtherLoc = hd.GetMonthlyWorkingHoursAtOtherLoc()
            });

            IsBusy = false;
        }

        #endregion


private bool _excludeFormerEmployees;
        public bool ExcludeFormerEmployees
        {
            get { return _excludeFormerEmployees; }
            set { Set(ref _excludeFormerEmployees, value); }
        }

在“生成报告”按钮命令中(这是员工报告)

   if (_excludeFormerEmployees) hd.ExcludeFormerEmployees();

在方法中:

 public void ExcludeFormerEmployees()
        { 
            RemoveAll(ef => Employee.IsDelegation==false&& Employee.Status==0);

        }

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您应该通过设置ICollectionView.Filter属性来使用集合视图过滤,而不是从集合中删除项目:

public bool ExcludeFormerEmployees
{
  get { return _excludeFormerEmployees; }
  set 
  { 
    Set(ref _excludeFormerEmployees, value); 

    // Get the colection view of the employee collection
    ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Employees);
    collectionView.Filter = 
      item => value 
        ? !(item as Employee).IsDelegation && (item as Employee).Status == 0 // Apply filter
        : true; // Clear filter and show all
  }
}