2013-06-13 39 views
5

Tôi có một hộp văn bản và một DataGrid. DataGrid có hai tên cột và địa chỉ Email. Tôi muốn lọc các giá trị datagrid với giá trị trong hộp văn bản. enter image description herelọc các giá trị dữ liệu wpf từ hộp văn bản

+0

Bằng cách đó cột, tên hoặc địa chỉ Email? Và bạn đang sử dụng mẫu thiết kế MVVM ở đây? – Colin

+0

@Colin, cách thực hiện điều này trong MVVM – Mussammil

Trả lời

22

Bạn có thể sử dụng một ICollectionView cho DataGridItemSource sau đó bạn có thể áp dụng một vị Filter và refesh danh sách khi cần thiết.

Đây là một ví dụ rất nhanh.

XAML:

<Window x:Class="WpfApplication10.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="188" Width="288" Name="UI" > 
    <StackPanel DataContext="{Binding ElementName=UI}"> 
     <TextBox Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged}" /> 
     <DataGrid ItemsSource="{Binding DataGridCollection}" /> 
    </StackPanel> 
</Window> 

Code:

namespace WpfApplication10 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     private ICollectionView _dataGridCollection; 
     private string _filterString; 

     public MainWindow() 
     { 
      InitializeComponent(); 
      DataGridCollection = CollectionViewSource.GetDefaultView(TestData); 
      DataGridCollection.Filter = new Predicate<object>(Filter); 
     } 

     public ICollectionView DataGridCollection 
     { 
      get { return _dataGridCollection; } 
      set { _dataGridCollection = value; NotifyPropertyChanged("DataGridCollection"); } 
     } 

     public string FilterString 
     { 
      get { return _filterString; } 
      set 
      { 
       _filterString = value; 
       NotifyPropertyChanged("FilterString"); 
       FilterCollection(); 
      } 
     } 

     private void FilterCollection() 
     { 
      if (_dataGridCollection != null) 
      { 
       _dataGridCollection.Refresh(); 
      } 
     } 

     public bool Filter(object obj) 
     { 
      var data = obj as TestClass; 
      if (data != null) 
      { 
       if (!string.IsNullOrEmpty(_filterString)) 
       { 
        return data.Name.Contains(_filterString) || data.Email.Contains(_filterString); 
       } 
       return true; 
      } 
      return false; 
     } 

     public IEnumerable<TestClass> TestData 
     { 
      get 
      { 
       yield return new TestClass { Name = "1", Email = "[email protected]" }; 
       yield return new TestClass { Name = "2", Email = "[email protected]" }; 
       yield return new TestClass { Name = "3", Email = "[email protected]" }; 
       yield return new TestClass { Name = "4", Email = "[email protected]" }; 
       yield return new TestClass { Name = "5", Email = "[email protected]" }; 
       yield return new TestClass { Name = "6", Email = "[email protected]" }; 
       yield return new TestClass { Name = "7", Email = "[email protected]" }; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      } 
     } 
    } 

    public class TestClass 
    { 
     public string Name { get; set; } 
     public string Email { get; set; } 
    } 
} 

Kết quả:

enter image description hereenter image description here

+0

Chúng tôi có thể thực hiện việc này trong mvvm – Mussammil

+0

Vui lòng xem thêm http://msdn.microsoft.com/de-de/library/ff407126(v=vs.110).aspx – peter70

Các vấn đề liên quan