本文介绍了从文本框中过滤 wpf 数据网格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个 Datagrid.数据网格有两列名称和电子邮件地址.我想用文本框中的值过滤数据网格值.

I have a textbox and a Datagrid. The datagrid has two columns name and Email address. I want to Filter the datagrid values with the value in the textbox.

推荐答案

你可以为 DataGrid ItemSource 使用一个 ICollectionView 然后你就可以应用 Filter 谓词并在需要时刷新列表.

You can use a ICollectionView for the DataGrid ItemSource then you can apply a Filter predicate and refesh the list when needed.

这是一个非常简单的例子.

Here is a very quick example.

XML:

<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>

代码:

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; }
    }
}

结果:

这篇关于从文本框中过滤 wpf 数据网格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:24