【问题标题】:filter wpf datagrid values from a textbox从文本框中过滤 wpf datagrid 值
【发布时间】:2013-06-13 23:46:59
【问题描述】:

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

【问题讨论】:

  • 按哪一列,姓名或电子邮件?你在这里使用 MVVM 设计模式吗?
  • @Colin ,如何在 MVVM 中执行此操作

标签: wpf wpfdatagrid wpf-4.0


【解决方案1】:

您可以将ICollectionView 用于DataGrid ItemSource,然后您可以应用Filter 谓词并在需要时刷新列表。

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

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>

代码:

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 = "1@test.com" };
                yield return new TestClass { Name = "2", Email = "2@test.com" };
                yield return new TestClass { Name = "3", Email = "3@test.com" };
                yield return new TestClass { Name = "4", Email = "4@test.com" };
                yield return new TestClass { Name = "5", Email = "5@test.com" };
                yield return new TestClass { Name = "6", Email = "6@test.com" };
                yield return new TestClass { Name = "7", Email = "7@test.com" };
            }
        }

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

结果:

【讨论】:

  • 我们可以在 mvvm 中做到这一点
  • 没关系,但您可以简单地使用最后一种方法。 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
猜你喜欢
  • 1970-01-01
  • 2011-05-09
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 2013-03-12
  • 1970-01-01
相关资源
最近更新 更多