WPF Filter a ListBox

后端 未结 3 1037
耶瑟儿~
耶瑟儿~ 2020-12-02 00:01

I have a ListBox with binding to a list of strings. I want to filter the list when I enter text in a TextBox. How can I do it?

publ         


        
3条回答
  •  青春惊慌失措
    2020-12-02 00:13

    If you set Dictionary as itemsource to listbox use the below code to sort,

        private void tb_filter_textChanged(object sender, TextChangedEventArgs e)
        {
            Dictionary dictObject = new Dictionary();
            ICollectionView view = CollectionViewSource.GetDefaultView(dictObject);
            view.Filter = CustomerFilter;
            listboxname.ItemsSource = view;
        }
    
        private bool CustomerFilter(object item)
        {
            KeyValuePair Items = (KeyValuePair) item;
            return Items.Value.ToString().Contains("a");
        }
    

    The above code returns the items contaning "a".

提交回复
热议问题