在文本框上过滤 DataGrid

发布于 2024-10-02 07:21:46 字数 1753 浏览 1 评论 0原文

我搜索示例或示例以通过文本框筛选 WPF DataGrid 列元素。

alt text

类似于 this (给定的示例使用 WPFToolkit...显然已被 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; }
}

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

alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-10-09 07:21:46

您可以通过将 DataGrid 绑定到支持过滤的 ICollectionView 来过滤 DataGrid 中的项目。

详细信息此处 对于 .NET 4。该过程与 .NET 4.5 相同,但似乎文档已丢失。有一个小提及 此处位于“分组、排序和过滤”标题下。

编辑:在最初撰写本文时,WPF 工具包尚未被 Microsoft 放弃。曾经属于它的控件现在位于框架中,并且该工具包仍然存在并且运行良好此处

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

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.

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

┈┾☆殇 2024-10-09 07:21:46

我在各个网站上都看到了关于此事的很多争论...

要使用数据表作为源来过滤后者作为数据网格,这很常见,可以使用以下代码:

DataTable dt = new DataTable("Table1");

//fill your datatable...

//after fill...
dataGrid1.DataContext = dt;
IBindingListView blv = dt.DefaultView;
blv.Filter = "NAME = 'MOISES'";

I have seen at various sites much ado about this matter...

To filter the latter being a datagrid using a datatable as the source, which is quite common to make the code below:

DataTable dt = new DataTable("Table1");

//fill your datatable...

//after fill...
dataGrid1.DataContext = dt;
IBindingListView blv = dt.DefaultView;
blv.Filter = "NAME = 'MOISES'";
々眼睛长脚气 2024-10-09 07:21:46

有多种解决方案,但在我看来,最好的解决方案是仅使用 DataGrid 样式而不发明新的继承 DataGird 类型的解决方案。以下是我发现的最好的:

There are several solutions, but in my opinion, the best solutions are the ones which uses only DataGrid styles without inventing a new inherited DataGird type. The followings are the best I found:

你在看孤独的风景 2024-10-09 07:21:46

我编写了自己的 FilterDataGrid 控件,它比 CodeProject 或其他地方提供的控件灵活得多。我既不能在这里发布完整的代码,也不能发布它。

但是:由于您的数据源很可能被包装到 ICollectionView 中,因此您可以执行以下操作:

    public void ApplyFilters()
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
        if (view != null)
        {
            view.Filter = FilterPredicate; 
        }
    }

    private bool FilterPredicate(object item)
    {
        var yourBoundItemOrRow = item as BoundItemType;

        return aFilterObject.Matches(yourBoundItemOrRow);
    }

您可以基于此概念轻松实现任何过滤器逻辑。甚至非常非常强大的过滤器。注意:我在自己的类中从数据网格派生了这些方法。它们也可以适应网格之外的工作,例如在 UserControl 中

I have written my own FilterDataGrid Control, it's much more flexible than the ones provided on CodeProject or elsewhere. I can neither post the full code here, nor can I publish it.

But: Since your datasource is most likely wrapped into a ICollectionView, you can do something like this:

    public void ApplyFilters()
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
        if (view != null)
        {
            view.Filter = FilterPredicate; 
        }
    }

    private bool FilterPredicate(object item)
    {
        var yourBoundItemOrRow = item as BoundItemType;

        return aFilterObject.Matches(yourBoundItemOrRow);
    }

You can implement any filter logic easily based on this concept. Even very, very powerful filters. Note: I have those methods in my own class derived from datagrid. They can be adapted to work outside of the grid, too, for example in a UserControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文