search string and filter in listview

c#, filtering, listview, wpf

Solution

You can use `ICollectionView` to binding your data to `ListView` and when you press the button you can filter this data.

Sample code:

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    public class MainViewModel : NotificationObject
    {
        public MainViewModel()
        {
            SearchPerson = new DelegateCommand(this.OnSearchPerson);        

            // test data
            _myDataSource.Add(new Person { Name = "John", Surname = "Blob" });
            _myDataSource.Add(new Person { Name = "Jack", Surname = "Smith" });
            _myDataSource.Add(new Person { Name = "Adam", Surname = "Jackson" });
        }

        private List<Person> _myDataSource = new List<Person>();

        private string _searchString;
        public string SearchString
        {
            get { return _searchString; }
            set { _searchString = value; RaisePropertyChanged(() => SearchPerson); }
        }

        private ICollectionView _people;
        public ICollectionView People
        {
            get { return CollectionViewSource.GetDefaultView(_myDataSource); }           
        }

        public ICommand SearchPerson { get; private set; }
        private void OnSearchPerson()
        {
            if (!string.IsNullOrEmpty(SearchString))
            {
                People.Filter = (item) => { return (item as Person).Name.StartsWith(SearchString) || (item as Person).Surname.StartsWith(SearchString) ? true : false; };
            }
            else
                People.Filter = null;
        }
    }

XAML file:

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding SearchString}" Width="150" />
            <Button Content="Search" Command="{Binding SearchPerson}" />
        </StackPanel>

        <ListView Grid.Row="1" ItemsSource="{Binding People}">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

Here is complete example (ListViewSearch.zip).

Problem

how can i search and filter close string in listview. for example i have two column in my listview and both are bindable. let say the first column named bindlname and second column name as "bindfname" for example this is my listview record: now if ever i type "Y and hit search button. it will filter YU ZIQ and FEAGS YAPSLE all first letter in lastname and firstname that starts with letter "Y" will be filtered... another if every i will type "UY" it will display UY QILUZ and UY ZKAE

Original source