创建一个TextBoxSearch从ListView WPF中过滤

本文关键字:ListView WPF 过滤 TextBoxSearch 一个 创建 | 更新日期: 2023-09-27 18:04:20

我正在创建一个应用程序,允许用户使用WPF向EntityFramework模型添加一些Employee详细信息。

到目前为止,我有一个ListView来表示员工姓名列表,当您选择员工姓名时,它会选择另一个ListView中的特定数据。我已经使用PredicateICollectionSource完成了这个任务。

但是我现在想要实现的,是拥有一个所谓的搜索引擎。因此,当用户在TextBox中输入员工姓名时,它会根据在搜索框中输入的内容过滤员工姓名。

我使用这个链接作为指南,但我不太确定如何在我自己的设计中实现它;在这个例子中,他们使用了ResourceArray

这就是我尝试的,使用Predicate;

    private EmployeeListViewModel()
        : base("")
    {
        EmployeeList = new ObservableCollection<EmployeeViewModel>(GetEmployees());
        this._view = new ListCollectionView(this.employeeList);
    }
    private ListCollectionView _view;
    public ICollectionView View
    {
         get { return this._view; }
    }
    private string _TextSearch;
    public string TextSearch
    {
        get { return _TextSearch; }
        set
        {
            _TextSearch = value;
            OnPropertyChanged("TextSearch");
            if (String.IsNullOrEmpty(value))
                View.Filter = null;
            else
                View.Filter = new Predicate<object>(o => ((EmployeeViewModel)o).FirstName == value);
        }
    }

in my view;

<TextBox Height="23" Name="txtSearch" VerticalAlignment="Bottom" Margin="70,0,0,183" Width="100" Grid.Row="1"
          Text="{Binding TextSearch, UpdateSourceTrigger=PropertyChanged}"/>

但是当我输入一些东西时,它会抛出这个异常;

Object reference not set to an instance of an object.

我的问题是,我如何实现这个才能让我过滤名字列表就像搜索框一样?

任何帮助将不胜感激或指导如何实现这一目标。

创建一个TextBoxSearch从ListView WPF中过滤

创建测试项目后,我无法重现您的异常

下面是我的工作代码:

MainWindow.xaml

<Window x:Class="gregory.bmclub.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Text="{Binding TextSearch,UpdateSourceTrigger=PropertyChanged}"/>
        <ListView Height="380" HorizontalAlignment="Left" Name="lsNames" VerticalAlignment="Top" Width="170" 
             ScrollViewer.VerticalScrollBarVisibility="Visible" 
             ScrollViewer.HorizontalScrollBarVisibility="Visible" 
             SelectedItem="{Binding SelectedEmployee}" 
             ItemsSource="{Binding View}" Grid.RowSpan="2" Grid.Row="1">
            <!--ItemsSource changed to "View"-->
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" Width="80" />
                    <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}" Width="80" />
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</Window>

MainWindow.cs

using System.Windows;
namespace gregory.bmclub
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new EmployeeListViewModel();
        }
    }
}

EmployeeViewModel.cs

namespace gregory.bmclub
{
    public class EmployeeViewModel
    {
        string firstname;
        public string FirstName
        {
            get { return firstname; }
            set { firstname = value; }
        }
    }
}

EmployeeListViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
namespace gregory.bmclub
{
    class EmployeeListViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
        public EmployeeListViewModel()//modified to public
        {
            EmployeeList = new ObservableCollection<EmployeeViewModel>(GetEmployees());
            this._view = new ListCollectionView(this.employeeList);
        }
        #region nonModifiedCode
        private ListCollectionView _employeeCol;
        public ICollectionView EmployeeCollection
        {
            get { return this._employeeCol; }
        }
        private ObservableCollection<EmployeeViewModel> employeeList;
        public ObservableCollection<EmployeeViewModel> EmployeeList
        {
            get { return employeeList; }
            set
            {
                employeeList = value;
                OnPropertyChanged("EmployeeList");
            }
        }
        private ListCollectionView _view;
        public ICollectionView View
        {
            get { return this._view; }
        }
        private string _TextSearch;
        public string TextSearch
        {
            get { return _TextSearch; }
            set
            {
                _TextSearch = value;
                OnPropertyChanged("TextSearch");
                if (String.IsNullOrEmpty(value))
                    View.Filter = null;
                else
                    View.Filter = new Predicate<object>(o => ((EmployeeViewModel)o).FirstName == value);
            }
        }
        #endregion
        //created for testing
        private List<EmployeeViewModel> GetEmployees()
        {
            var mylist = new List<EmployeeViewModel>();
            mylist.Add(new EmployeeViewModel() { FirstName = "nummer1" });
            mylist.Add(new EmployeeViewModel() { FirstName = "nummer2" });
            return mylist;
        }
    }
}

我有以下代码与我一起工作,但我不得不放弃Textsearch方法,并应用了一个不同的方法,我添加了视图行代码,希望能使你的代码工作。

private EmployeeListViewModel()
    : base("")
{
    EmployeeList = new ObservableCollection<EmployeeViewModel>(GetEmployees());
    this._view = new ListCollectionView(this.employeeList);
     myEmployeeList = new CollectionViewSource();
        myEmployeeList.Source = this.EscortList;
        myEmployeeList.Filter += ApplyFilter;
}
    internal CollectionViewSource employeeList { get; set; }
        internal CollectionViewSource myEmployeeList { get; set; }
        private ObservableCollection<EmployeeViewModel> employeeList;
    public ObservableCollection<EmployeeViewModel> EmployeeList
    {
        get { return employeeList; }
        set
        {
            employeeList = value;
            OnPropertyChanged("EmployeeList");
        }
    }
private ListCollectionView _view;
// the collection below is the collection you will need to be your listview itemsource {Binding View}
public ICollectionView View
{
     //you need to return your CollectionViewSource here
     get { return myEmployeeList._view; }
}
// you need to use the following filtering methods as it did work for methods
 private void OnFilterChanged()
    {
        myEmployeeList.View.Refresh();
    }
    private string filter;
    public string Filter
    {
        get { return this.filter; }
        set
        {
            this.filter = value;
            OnFilterChanged();
        }
    }
    void ApplyFilter(object sender, FilterEventArgs e)
    {
        EmployeeViewModel svm = (EmployeeViewModel)e.Item;
        if (string.IsNullOrWhiteSpace(this.Filter) || this.Filter.Length == 0)
        {
            e.Accepted = true;
        }
        else
        {
        // you can change the property you want to search your model
            e.Accepted = svm.Surname.Contains(Filter);
        }
    }
这里是我的Xaml代码绑定到Listview
<ListView Name="lsvEscort" HorizontalAlignment="Left" Height="297" ItemsSource="{Binding View}">

这是我的文本搜索过滤器绑定路径

<TextBox x:Name="txtSearch"  Grid.Column="1" Text="{Binding Path=Filter,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

希望这能解决你的问题