繁体   English   中英

如何根据 CheckBox 值 (MVVM) 过滤集合

[英]How to filter a collection based on CheckBox value (MVVM)

我有一个员工表。 我有一个 WPF 屏幕。 在屏幕中,我想要一个“排除前雇员”复选框(默认选中),当它选中我生成报告时,表中没有前雇员。 但是当我删除签入复选框时,我想看到前雇员。

在我的 xaml 中看到:

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

在视图模型中:

我的生成报告按钮命令(获取员工报告。)

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);

        }

谢谢你的帮助。

您应该通过设置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
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM