在WPF中过滤DataGrid [英] Filter a DataGrid in WPF

查看:232
本文介绍了在WPF中过滤DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  dataGrid1.Items.Add(model); 

模型成为数据库中的数据。它有一个 Id(int) Name(string) Text(string)



在我的datagrid中,我只显示模型的名称。当我在文本框中输入内容时,现在可以如何过滤datagrid?



我在这个页面上: http://msdn.microsoft.com/en-us/library/vstudio/ff407126(v = vs .100).aspx ,但我不明白那里的代码,我无法解释我应该如何处理我的问题。

解决方案

有多种方式来过滤集合



我们建议这是您的项目类

  public class Model 
{
public string Name
{
get;
设置;
}
}

,您的收藏看起来像

  var ObColl = new ObservableCollection< Model>(); 

ObColl.Add(new Model(){Name =John});
ObColl.Add(new Model(){Name =Karl});
ObColl.Add(new Model(){Name =Max});
ObColl.Add(new Model(){Name =Mary});



方式1(谓词):



  public MainWindow()
{
InitializeComponent();

//收集您的ObservableCollection
var _itemSourceList = new CollectionViewSource(){Source = ObColl};

// ICollectionView View / UI部分
ICollectionView Itemlist = _itemSourceList.View;

//您的Filter
var yourCostumFilter = new Predicate< object>(item =>((Model)item).Name.Contains(Max));

//现在我们添加过滤器
Itemlist.Filter = yourCostumFilter;

dataGrid1.ItemsSource = Itemlist;
}



Way 2(FilterEventHandler):



  public MainWindow()
{
InitializeComponent();

//收集你的过滤器
var _itemSourceList = new CollectionViewSource(){Source = ObColl};

//现在我们添加我们的Filter
_itemSourceList.Filter + = new FilterEventHandler(yourFilter);

// ICollectionView View / UI部分
ICollectionView Itemlist = _itemSourceList.View;

dataGrid1.ItemsSource = Itemlist;
}

private void yourFilter(object sender,FilterEventArgs e)
{
var obj = e.Item as Model;
if(obj!= null)
{
if(obj.Name.Contains(Max))
e.Accepted = true;
else
e.Accepted = false;
}
}



扩展信息到路1



如果需要多个条件或一些复杂的过滤器,您可以向Predicat添加一个方法

  / /你的过滤器
var yourComplexFilter = new Predicate< object>(ComplexFilter);

private bool ComplexFilter(object obj)
{
//您的逻辑
}


I load a lists of objects in a datagrid with this:

dataGrid1.Items.Add(model);

The model become data from a database. It has a Id(int), Name(string) and Text(string)

In my datagrid I show only the Name of the model. How can I filter the datagrid now, when I enter something in a textbox?

I was at this page: http://msdn.microsoft.com/en-us/library/vstudio/ff407126(v=vs.100).aspx but I don't understand the code from there and I can not explain how I should transpose that for my problem.

解决方案

there are multiple way's to filter Collection

let's suggesting this is your Item Class

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

and your collection looks like

       var ObColl = new ObservableCollection<Model>();

        ObColl.Add(new Model() { Name = "John" });
        ObColl.Add(new Model() { Name = "Karl" });
        ObColl.Add(new Model() { Name = "Max" });
        ObColl.Add(new Model() { Name = "Mary" });

Way 1 (Predicate):

    public MainWindow()
    {
        InitializeComponent();

        // Collection which will take your ObservableCollection
        var _itemSourceList = new CollectionViewSource() { Source = ObColl };

        // ICollectionView the View/UI part 
        ICollectionView Itemlist = _itemSourceList.View;

        // your Filter
        var yourCostumFilter= new Predicate<object>(item => ((Model)item).Name.Contains("Max"));

        //now we add our Filter
        Itemlist.Filter = yourCostumFilter;

        dataGrid1.ItemsSource = Itemlist;
    }

Way 2 (FilterEventHandler):

    public MainWindow()
    {
        InitializeComponent();

        // Collection which will take your Filter
        var _itemSourceList = new CollectionViewSource() { Source = ObColl };

       //now we add our Filter
       _itemSourceList.Filter += new FilterEventHandler(yourFilter);

        // ICollectionView the View/UI part 
        ICollectionView Itemlist = _itemSourceList.View;

        dataGrid1.ItemsSource = Itemlist;
    }

    private void yourFilter(object sender, FilterEventArgs e)
    {
        var obj = e.Item as Model;
        if (obj != null)
        {
            if (obj.Name.Contains("Max"))
                e.Accepted = true;
            else
                e.Accepted = false;
        }
    }

extended Information to Way 1

if need multiple conditions or some complex Filter you can add a method to your Predicat

    // your Filter
    var yourComplexFilter= new Predicate<object>(ComplexFilter);

    private bool ComplexFilter(object obj)
    {
        //your logic
    }

这篇关于在WPF中过滤DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆