在文本框上过滤 DataGrid
我搜索示例或示例以通过文本框筛选 WPF DataGrid 列元素。
类似于 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过将 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
我在各个网站上都看到了关于此事的很多争论...
要使用数据表作为源来过滤后者作为数据网格,这很常见,可以使用以下代码:
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:
有多种解决方案,但在我看来,最好的解决方案是仅使用 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 inheritedDataGird
type. The followings are the best I found:我编写了自己的 FilterDataGrid 控件,它比 CodeProject 或其他地方提供的控件灵活得多。我既不能在这里发布完整的代码,也不能发布它。
但是:由于您的数据源很可能被包装到 ICollectionView 中,因此您可以执行以下操作:
您可以基于此概念轻松实现任何过滤器逻辑。甚至非常非常强大的过滤器。注意:我在自己的类中从数据网格派生了这些方法。它们也可以适应网格之外的工作,例如在 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:
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