wpf 在文本框上过滤 DataGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4166403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter a DataGrid on a Text box
提问by serhio
I search an example or sample to filter the WPF DataGrid column elements by a textbox.
我搜索了一个示例或示例,以通过文本框过滤 WPF DataGrid 列元素。
Something similar to this(the given example uses a WPFToolkit... apparently abandoned by Microsoft...)
与此类似的东西(给定的示例使用WPFToolkit......显然被微软放弃......)
XAML
XAML
<Canvas>
<DataGrid Height="200" Name="dataGrid1" Width="200" Canvas.Top="23" />
<TextBox Name="textBox1" Width="120" />
</Canvas>
cs:
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; }
}
采纳答案by vlad
You can filter the Items in the DataGrid by binding it to an ICollectionView
that supports filtering.
您可以通过将 DataGrid 绑定到ICollectionView
支持过滤的来过滤DataGrid 中的项目。
Details herefor .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 hereunder the "Grouping, Sorting, and Filtering" heading.
.NET 4 的详细信息在这里。 .NET 4.5 的过程是相同的,但文档似乎已丢失。有一个小提它这里的“分组,排序与过滤”标题下。
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 工具包还没有被微软抛弃。曾经是它的一部分的控件现在在框架中,工具包在这里还活着并且运行良好
回答by Moises Marques
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'";
回答by Mohammed A. Fadil
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:
有几种解决方案,但在我看来,最好的解决方案是只使用DataGrid
样式而不发明新的继承DataGird
类型。以下是我发现的最好的:
- Option 1: which I personally use: Automatic WPF Toolkit DataGrid Filtering
- Option 2: Auto-filter for Microsoft WPF DataGrid
- 选项 1:我个人使用的:Automatic WPF Toolkit DataGrid Filtering
- 选项 2: Microsoft WPF DataGrid 的自动过滤器
回答by Falcon
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.
我已经编写了自己的 FilterDataGrid 控件,它比 CodeProject 或其他地方提供的控件灵活得多。我既不能在这里发布完整的代码,也不能发布它。
But: Since your datasource is most likely wrapped into a ICollectionView, you can do something like this:
但是:由于您的数据源很可能包含在 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);
}
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
您可以基于此概念轻松实现任何过滤器逻辑。甚至非常非常强大的过滤器。注意:我在我自己的类中从 datagrid 派生了这些方法。它们也可以适应在网格之外工作,例如在 UserControl 中