从文本框中过滤 wpf 数据网格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17099042/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:00:25  来源:igfitidea点击:

filter wpf datagrid values from a textbox

wpfwpfdatagridwpf-4.0

提问by Jayant Rao

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.enter image description here

我有一个文本框和一个 Datagrid。数据网格有两列名称和电子邮件地址。我想用文本框中的值过滤数据网格值。在此处输入图片说明

回答by sa_ddam213

You can use a ICollectionViewfor the DataGridItemSourcethen you can apply a Filterpredicate and refesh the list when needed.

您可以使用ICollectionViewforDataGridItemSource然后您可以应用Filter谓词并在需要时刷新列表。

Here is a very quick example.

这是一个非常快速的示例。

Xaml:

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>

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

Result:

结果:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明