WPF数据绑定数据网格筛选/搜索
我有一个WPF应用程序,它使用(当前)本地数据库作为绑定源。使用VisualStudio2010工具,我有一个LINQ-SQL模型,它充当大多数表单的数据上下文 我有一个带有文本框和数据网格的用户控件。datagrid ItemSource是在UserControl.Loaded事件和表上设置的。TextBox分配了一个事件,以便在文本更改时在数据库上执行查询,并且在datagrid上更新ItemSource 问题在于查询数据库所需的时间。当我为每次搜索重新分配DataGrid itemsource时WPF数据绑定数据网格筛选/搜索,wpf,database,datagrid,itemsource,Wpf,Database,Datagrid,Itemsource,我有一个WPF应用程序,它使用(当前)本地数据库作为绑定源。使用VisualStudio2010工具,我有一个LINQ-SQL模型,它充当大多数表单的数据上下文 我有一个带有文本框和数据网格的用户控件。datagrid ItemSource是在UserControl.Loaded事件和表上设置的。TextBox分配了一个事件,以便在文本更改时在数据库上执行查询,并且在datagrid上更新ItemSource 问题在于查询数据库所需的时间。当我为每次搜索重新分配DataGrid itemsour
代码:
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用Tracker.Model;
名称空间跟踪器
{
///
///DocumentsView.xaml的交互逻辑
///
公共部分类DocumentsView:UserControl
{
专用TrackerDataContext数据库;
公共文档视图()
{
初始化组件();
this.Loaded+=新的路由EventHandler(文档视图已加载);
}
已加载无效文档视图(对象发送方、路由目标)
{
db=新的TrackerDataContext();
dataGrid1.ItemsSource=db.Documents;
}
私有void txtSearchBox_TextChanged(对象发送者,textchangedventargs e)
{
TextBox TextBox=发送者作为TextBox;
如果(文本框!=null)
{
字符串searchstr=textbox.Text;
如果(!string.IsNullOrEmpty(searchstr))
{
var filtered=来自db.Documents中的文档
where document.Subject.Contains(searchstr)
||document.Reference.Contains(searchstr)
选择文档;
dataGrid1.ItemsSource=已过滤;
}
其他的
{
dataGrid1.ItemsSource=db.Documents;
}
}
}
}
}
我认为您应该一开始就从数据库中加载所有记录
然后在ItemsSource上使用ICollectionView.Filter。
这样,您就不必进行数据库事务
你应该写些那样的东西
private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox != null)
{
_searchstr = textbox.Text;
if (!string.IsNullOrEmpty(_searchstr))
{
ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Filter = new Predicate<object>(filter);
}
}
}
private bool filter(object item)
{
if(item.Subject.Contains(_searchstr) || item.Reference.Contains(searchstr))
{
return true;
}
return false;
}
private void txtSearchBox\u TextChanged(对象发送者,textchangedventargs e)
{
TextBox TextBox=发送者作为TextBox;
如果(文本框!=null)
{
_searchstr=textbox.Text;
如果(!string.IsNullOrEmpty(_searchstr))
{
ICollectionView视图=CollectionViewSource.GetDefaultView(ItemsSource);
view.Filter=新谓词(Filter);
}
}
}
专用布尔过滤器(对象项)
{
if(item.Subject.Contains(_searchstr)| item.Reference.Contains(searchstr))
{
返回true;
}
返回false;
}
希望这有帮助,
Nidal.小更正,您只需要检查_searchStr中的NULL,否则一旦开始删除搜索的字符,列表将不会重新填充。
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tracker.Model;
namespace Tracker
{
/// <summary>
/// Interaction logic for DocumentsView.xaml
/// </summary>
public partial class DocumentsView : UserControl
{
private TrackerDataContext db;
public DocumentsView()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DocumentsView_Loaded);
}
void DocumentsView_Loaded(object sender, RoutedEventArgs e)
{
db = new TrackerDataContext();
dataGrid1.ItemsSource = db.Documents;
}
private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox != null)
{
string searchstr = textbox.Text;
if (!string.IsNullOrEmpty(searchstr))
{
var filtered = from document in db.Documents
where document.Subject.Contains(searchstr)
|| document.Reference.Contains(searchstr)
select document;
dataGrid1.ItemsSource = filtered;
}
else
{
dataGrid1.ItemsSource = db.Documents;
}
}
}
}
}
private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (textbox != null)
{
_searchstr = textbox.Text;
if (!string.IsNullOrEmpty(_searchstr))
{
ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Filter = new Predicate<object>(filter);
}
}
}
private bool filter(object item)
{
if(item.Subject.Contains(_searchstr) || item.Reference.Contains(searchstr))
{
return true;
}
return false;
}