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. lọc các giá trị dữ liệu wpf từ hộp văn bản
5
A
Trả lời
22
Bạn có thể sử dụng một ICollectionView
cho DataGrid
ItemSource
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ả:
Các vấn đề liên quan
- 1. Nhận các giá trị đầu vào từ hộp văn bản
- 2. Cho phép 'dán dữ liệu' vào hộp văn bản WPF
- 3. Giá trị hộp văn bản ActiveX
- 4. bảng dữ liệu tạo hộp kiểm lọc
- 5. Xóa các giá trị dữ liệu trong wpf
- 6. cột hộp tổ hợp dữ liệu wpf
- 7. Ràng buộc trong cột văn bản lưới dữ liệu WPF
- 8. Cách lấy giá trị văn bản cụ thể từ hộp văn bản dựa trên vị trí chuột
- 9. WPF: Lấy văn bản "gói" ra khỏi hộp văn bản
- 10. Trả về giá trị tùy chỉnh từ hộp thoại WPF
- 11. Đọc giá trị hộp văn bản ASP.NET với JavaScript
- 12. Bao bì hộp văn bản WPF
- 13. wpf: Nút, Hộp văn bản, bị cắt
- 14. Lọc khung dữ liệu theo giá trị trong cột
- 15. WPF giá trị combobox và hiển thị văn bản
- 16. lấy giá trị từ hộp văn bản trong vòng lặp asp.net C#
- 17. Gán các giá trị cho ô dữ liệu tham chiếu
- 18. Cách nhận giá trị hộp văn bản trong ember.js
- 19. WPF: Hộp văn bản với ProgressBar
- 20. Hộp văn bản WPF Kéo dài
- 21. Xác thực Hộp văn bản WPF
- 22. Lấy các giá trị từ bảng trường văn bản javascript
- 23. Làm cách nào để sao chép dữ liệu nhập từ hộp văn bản này sang hộp văn bản khác qua hộp kiểm bằng jQuery?
- 24. Javascript - nhận giá trị từ hộp văn bản ở mọi phím bấm
- 25. Cách tắt các phím tắt trong Hộp văn bản WPF
- 26. dữ liệu lọc JSON
- 27. lấy dữ liệu từ giá trị khóa động trong json
- 28. Dữ liệu ràng buộcĐể vào hộp văn bản
- 29. Cách lọc một giá trị tập dữ liệu với một tập dữ liệu Giá trị bảng khác trong SSRS
- 30. Tạo điều khiển hộp văn bản WPF như hộp văn bản người nhận email Outlook
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
@Colin, cách thực hiện điều này trong MVVM – Mussammil