创建一个TextBoxSearch以从ListView WPF中过滤

时间:2013-03-18 09:10:01

标签: c# wpf listview mvvm

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

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

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

我使用This Link作为指导,但我不太清楚如何在我自己的设计中实现它;在示例中,他们使用了Resource并使用了Array

这是我尝试过的,使用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);
        }
    }

在我看来;

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

所以我的问题是,如何实现这一点,以便它实际上允许我过滤搜索框中的名称列表?

任何帮助都会感激或指导如何实现这一目标。

2 个答案:

答案 0 :(得分:7)

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

这是我的工作代码:

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

答案 1 :(得分:2)

我有以下代码与我一起工作,但我不得不放弃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);
        }
    }

这是我绑定到Listview的Xaml代码

<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}" />

希望这可以解决您的问题