WPF DataGrid过滤器 [英] WPF DataGrid filter

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

问题描述

我搜索一个示例或示例来过滤WPF DataGrid列元素的一个文本框。

I search an example or sample to filter the WPF DataGrid column elements ty a textbox.

类似于这个(给定的例子使用了一个显然被Microsoft放弃的WPFToolkit ...)

Something similar to this (the given example uses a WPFToolkit... apparently abandoned by Microsoft...)

编辑

代码更新

XAML

<Canvas>
    <DataGrid Height="200" Name="dataGrid1" Width="200" Canvas.Top="23" />
    <TextBox  Name="textBox1" Width="120" />
</Canvas>

cs:

public partial class MainWindow : Window
{
    private List<Personne> persons;
    ICollectionView cvPersonnes;

    public MainWindow()
    {
        InitializeComponent();

        persons = new List<Personne>();

        persons.Add(new Personne() { Id = 1, Nom = "Jean-Michel", Prenom = "BADANHAR" });
        persons.Add(new Personne() { Id = 1, Nom = "Gerard", Prenom = "DEPARDIEU" });
        persons.Add(new Personne() { Id = 1, Nom = "Garfild", Prenom = "THECAT" });
        persons.Add(new Personne() { Id = 1, Nom = "Jean-Paul", Prenom = "BELMONDO" });

        cvPersonnes = CollectionViewSource.GetDefaultView(persons);

        if (cvPersonnes != null)
        {
            dataGrid1.AutoGenerateColumns = true;
            dataGrid1.ItemsSource = cvPersonnes;
            cvPersonnes.Filter = TextFilter;
        }
    }

    public bool TextFilter(object o)
    {
        Personne p = (o as Personne);
        if (p == null) 
            return false;

        if (p.Nom.Contains(textBox1.Text))
            return true;
        else
            return false;
    }

}

public class Personne
{
    public int Id { get; set; }
    public string Nom { get; set; }
    public string Prenom { get; set; }
}


推荐答案

在DataGrid中将其绑定到支持过滤的 ICollectionView

You can filter the Items in the DataGrid by binding it to an ICollectionView that supports filtering.

详细信息这里 for .NET 4.过程是对于.NET 4.5来说是一样的,但似乎文档已经丢失。有一点点提到它这里在分组,排序和过滤标题下。

Details here for .NET 4. The process is the same for .NET 4.5, but it seems the documentation has been lost. There's a small mention to it here under the "Grouping, Sorting, and Filtering" heading.

编辑:在最初写的时候,WPF工具包没有被放弃微软。过去是其中一部分的控件现在在框架中,该工具包还活着,效果很好 here

edit: at the time this was originally written, the WPF toolkit had not been abandoned by Microsoft. The controls that used to be part of it are now in the framework, and the toolkit was alive and doing well here

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

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