Simple WPF combobox filter

半世苍凉 提交于 2019-12-17 19:07:08

问题


I have searched Google for a simple solution to this but no luck. I have a standard WPF combo box which I would simply like to be able to filter the list displayed according to the first 2 or 3 letters a users types when the combo box has focus. I tried some coding including some lamba expressions but the error "System.NotSupportedException" keeps getting thrown on the line where "combobox.Items.Filter" is specified. I'm not using MVVM and would just like this simple functionality available for the user. Please help! P.S. IsEditable, IsTextSearchEnabled and StaysOpenOnEdit properties are set to true but the desired functionality is not yet achieved.


回答1:


I have developed a sample application. I have used string as record item, you can do it using your own entity. Backspace also works properly.

 public class FilterViewModel
    {
        public IEnumerable<string> DataSource { get; set; }       

        public FilterViewModel()
        {
            DataSource = new[] { "india", "usa", "uk", "indonesia" };           
        }
    }

public partial class WinFilter : Window
    {
          public WinFilter()
          {
             InitializeComponent();

             FilterViewModel vm = new FilterViewModel();
             this.DataContext = vm;
          }

          private void Cmb_KeyUp(object sender, KeyEventArgs e)
          {
              CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(Cmb.ItemsSource);

              itemsViewOriginal.Filter = ((o) =>
              {
                  if (String.IsNullOrEmpty(Cmb.Text)) return true;
                  else
                  {
                     if (((string)o).Contains(Cmb.Text)) return true;
                     else return false;
                  }
              });

             itemsViewOriginal.Refresh();

             // if datasource is a DataView, then apply RowFilter as below and replace above logic with below one
             /* 
              DataView view = (DataView) Cmb.ItemsSource; 
              view.RowFilter = ("Name like '*" + Cmb.Text + "*'"); 
             */
          }
     }

XAML

<ComboBox x:Name="Cmb"
          IsTextSearchEnabled="False"
          IsEditable="True"
          ItemsSource="{Binding DataSource}"
          Width="120"
          IsDropDownOpen="True"
          StaysOpenOnEdit="True"
          KeyUp="Cmb_KeyUp" />




回答2:


I think the CollectionView is what you are looking for.

  public ObservableCollection<NdfClassViewModel> Classes
  {
      get { return _classes; }
  }

  public ICollectionView ClassesCollectionView
  {
      get
      {
          if (_classesCollectionView == null)
          {
              BuildClassesCollectionView();
          }
           return _classesCollectionView;
      }
  }

  private void BuildClassesCollectionView()
  {
      _classesCollectionView = CollectionViewSource.GetDefaultView(Classes);
      _classesCollectionView.Filter = FilterClasses;

      OnPropertyChanged(() => ClassesCollectionView);
  }

  public bool FilterClasses(object o)
  {
      var clas = o as NdfClassViewModel;



      // return true if object should be in list with applied filter, return flase if not
  }

You wanna use the "ClassesCollectionView" as your ItemsSource for your Combobox



来源:https://stackoverflow.com/questions/34382459/simple-wpf-combobox-filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!