过滤项目来源

Filter itemsource

通过这段代码,我设置了数据网格的 Itemsource。但是,我得到了更多的 wpf 控件,这些控件需要更多地过滤数据网格,例如从一个时间范围。我可以为此编写一个新查询,但这似乎没有必要,因为数据已经可用,我只需要对其进行过滤。但是最好的方法是什么?

非常感谢能得到的任何帮助!

DateTime dateStart = CalenderSearch.SelectedDates.First();
DateTime dateEnd = CalenderSearch.SelectedDates.Last();

ObjectQuery<Fouten> fouten = eventsEntities.Foutens;

                var query =
                (from fout in fouten
                 where dateStart <= fout.Datum && dateEnd >= fout.Datum && fout.Rapporten.Treinen.NameTrein == trein.NameTrein
                 orderby fout.Datum, fout.Time


                 select new
                 {
                     Datum = fout.Datum,
                     Time = fout.Time,
                     FoutCode = fout.FoutCode,
                     Omschrijving = fout.Omschrijving,
                     Teller = fout.Teller,
                     Module = fout.Module,
                     FoutId = fout.FoutId

                 }).AsEnumerable().Select(x => new Fouten
                 {
                     Datum = x.Datum,
                     Time = x.Time,
                     FoutCode = x.FoutCode,
                     Omschrijving = x.Omschrijving,
                     Teller = x.Teller,
                     Module = x.Module,
                     FoutId = x.FoutId
                 }).ToList();

if (query.Count == 0)
                    foutensDataGrid.ItemsSource = null;
                else
                    foutensDataGrid.ItemsSource = query;

您可以将加载的数据保存在 class 字段中,然后通过 handle buttonClicks 对其进行过滤:

public class YourView
{
    private List<Fouten> loadedData;
    public void LoadData(...)
    {
        ObjectQuery<Fouten> fouten = eventsEntities.Foutens;

        // here you save unfiltered data to the field and then you can use it to filter collection
        loadedData = ...;

        // if you want to filter values immediately you can call filter method right here
        // FilterByFoutCode(someValue);

        if (loadedData.Count == 0)
            foutensDataGrid.ItemsSource = null;
        else
            foutensDataGrid.ItemsSource = loadedData;
    }

    private void FilterByFoutCodeButtonClick(object sender, EventArgs e)
    {
        var filter = FoutCodeTextBox.Content.ToString();
        if(!string.IsNullOrEmpty(filter))
        {
            // if your filter is not empty then filter loadedData by criteria 
            FilterByFoutCode(filter);
        }
    }

    private void FilterByFoutCode(string filter)
    {
        foutensDataGrid.ItemsSource = loadedData.Where(x => x.FoutCode == filter);
    }
}

此外,在性能上下文中,您不应通过 select new {...}.AsEnumerable()Select(...) 对集合进行额外迭代,只需在 LINQ 查询后立即调用 ToList() .

最后 MVVM pattern 是 WPF 的标准事实,它对您的应用程序很有用。

在 WPF 中,在 GUI 元素中用作 ItemsSource 的每个集合都有一个与之关联的 ICollectionView
ICollectionView 有一个 Filter 属性,类型为 Predicate<object>

如果您设置此 Filter 并在之后调用 Refresh(),DataGrid 将自行更新以仅显示 Filter 返回 true 的项目。

如何使用它的示例:

var collectionView = CollectionViewSource.GetDefaultView(foutensDataGrid.ItemsSource);
collectionView.Filter = o => {
    var fouten = o as Fouten;
    //do your filtering, e.g.
    return fouten.Datum <= dateEnd && fouten.Datum >= dateStart;
}
collectionView.Refresh();