c# - 具有过滤器和自动完成功能的组合框

标签 c# wpf mvvm combobox autocomplete

有没有人成功使用 WPF 的 ComboBox 自动完成和过滤功能?我现在已经花了几个小时,但无法确定它。这是 WPF + MVVM Light。这是我的设置。

虚拟机层

提供以下属性的 ViewModel:

  • FilterText ( string ):用户在 TextBox 区域中输入的用于过滤的文本。在 FilteredItems 上触发更改通知.
  • Items ( List<string> ):这是包含所有选项的主要数据源。
  • FilteredItems : Items 的过滤列表使用 FilterText .
  • SelectedOption ( string ): 当前选择的选项。

  • 查看图层

    一个组合框,用户只能从下拉选项中进行选择。但是,应该允许用户在文本框区域中键入文本,并且下拉列表应该过滤掉不以键入的文本开头的项目。第一个匹配项应自动附加到文本框(即自动完成)。这是我的绑定(bind):
  • ItemsSource : 绑定(bind)到 FilteredItems , 单向
  • Text绑定(bind)到 FilterText , 双向
  • SelectedItem绑定(bind)到 SelectedOption , 双向
  • IsTextSearchEnabled设置为 true 以启用自动完成。

    此设置的问题在于,一旦用户键入第一个字母,就会触发自动完成并尝试找到第一个匹配条目,如果找到,则设置 SelectedItem到该条目,其中设置 Text ComboBox 的属性(property)到该项目,这反过来会触发过滤操作,并且下拉列表中只剩下一个与 Text 完全匹配的条目,这不是它应该的样子。

    例如,如果用户键入“C”,自动完成将尝试定位以“C”开头的第一个条目。假设第一个匹配条目是“客户”。自动完成将选择该条目,它将设置 SelectedItem到“客户”,因此 Text也将变为“客户。这将调用 FilterText 因为绑定(bind),这将更新 FilteredItems ,现在将只返回一个条目,而不是返回所有以“C”开头的条目。

    我在这里想念什么?

    最佳答案

    我认为你的方法太复杂了。
    您可以实现一个简单的附加行为来实现过滤建议列表,同时启用自动完成。

    除了 ComboBox.ItemsSource 的公共(public)源集合之外,此示例不需要任何其他属性。 .过滤是通过使用 ICollectionView.Filter 来完成的。属性(property)。这将仅修改 ItemsControl 的内部源集合的 View 。 ,但不是底层绑定(bind)源集合本身。设置IsTextSearchEnabledTrue不需要启用自动完成。

    基本思想是在 TextBox.TextChanged 上触发过滤。比 ComboBox.SelectedItemChanged (或 ComboBox.SelectedItem 一般)。

    组合框.cs

    class ComboBox : DependencyObject
    {
      #region IsFilterOnAutoCompleteEnabled attached property
    
      public static readonly DependencyProperty IsFilterOnAutocompleteEnabledProperty =
        DependencyProperty.RegisterAttached(
          "IsFilterOnAutocompleteEnabled",
          typeof(bool),
          typeof(ComboBox),
          new PropertyMetadata(default(bool), ComboBox.OnIsFilterOnAutocompleteEnabledChanged));
    
      public static void SetIsFilterOnAutocompleteEnabled(DependencyObject attachingElement, bool value) =>
        attachingElement.SetValue(ComboBox.IsFilterOnAutocompleteEnabledProperty, value);
    
      public static bool GetIsFilterOnAutocompleteEnabled(DependencyObject attachingElement) =>
        (bool)attachingElement.GetValue(ComboBox.IsFilterOnAutocompleteEnabledProperty);
    
      #endregion
    
      // Use hash tables for faster lookup
      private static Dictionary<TextBox, System.Windows.Controls.ComboBox> TextBoxComboBoxMap { get; }
      private static Dictionary<TextBox, int> TextBoxSelectionStartMap { get; }
      private static Dictionary<System.Windows.Controls.ComboBox, TextBox> ComboBoxTextBoxMap { get; }
      private static bool IsNavigationKeyPressed { get; set; }
    
      static ComboBox()
      {
        ComboBox.TextBoxComboBoxMap = new Dictionary<TextBox, System.Windows.Controls.ComboBox>();
        ComboBox.TextBoxSelectionStartMap = new Dictionary<TextBox, int>();
        ComboBox.ComboBoxTextBoxMap = new Dictionary<System.Windows.Controls.ComboBox, TextBox>();
      }
    
      private static void OnIsFilterOnAutocompleteEnabledChanged(
        DependencyObject attachingElement,
        DependencyPropertyChangedEventArgs e)
      {
        if (!(attachingElement is System.Windows.Controls.ComboBox comboBox
          && comboBox.IsEditable))
        {
          return;
        }
    
        if (!(bool)e.NewValue)
        {
          ComboBox.DisableAutocompleteFilter(comboBox);
          return;
        }
    
        if (!comboBox.IsLoaded)
        {
          comboBox.Loaded += ComboBox.EnableAutocompleteFilterOnComboBoxLoaded;
          return;
        }
        ComboBox.EnableAutocompleteFilter(comboBox);
      }
    
      private static async void FilterOnTextInput(object sender, TextChangedEventArgs e)
      {
        await Application.Current.Dispatcher.InvokeAsync(
          () =>
          {
            if (ComboBox.IsNavigationKeyPressed)
            {
              return;
            }
    
            var textBox = sender as TextBox;
            int textBoxSelectionStart = textBox.SelectionStart;
            ComboBox.TextBoxSelectionStartMap[textBox] = textBoxSelectionStart;
    
            string changedTextOnAutocomplete = textBox.Text.Substring(0, textBoxSelectionStart);
            if (ComboBox.TextBoxComboBoxMap.TryGetValue(
              textBox,
              out System.Windows.Controls.ComboBox comboBox))
            {
              comboBox.Items.Filter = item => item.ToString().StartsWith(
                changedTextOnAutocomplete,
                StringComparison.OrdinalIgnoreCase);
            }
          },
          DispatcherPriority.Background);
      }
    
      private static async void HandleKeyDownWhileFiltering(object sender, KeyEventArgs e)
      {
        var comboBox = sender as System.Windows.Controls.ComboBox;
        if (!ComboBox.ComboBoxTextBoxMap.TryGetValue(comboBox, out TextBox textBox))
        {
          return;
        }
    
        switch (e.Key)
        {
          case Key.Down 
            when comboBox.Items.CurrentPosition < comboBox.Items.Count - 1 
                 && comboBox.Items.MoveCurrentToNext():
          case Key.Up 
            when comboBox.Items.CurrentPosition > 0 
                 && comboBox.Items.MoveCurrentToPrevious():
          {
            // Prevent the filter from re-apply as this would override the
            // current selection start index
            ComboBox.IsNavigationKeyPressed = true;
    
            // Ensure the Dispatcher en-queued delegate 
            // (and the invocation of the SelectCurrentItem() method)
            // executes AFTER the FilterOnTextInput() event handler.
            // This is because key input events have a higher priority
            // than text change events by default. The goal is to make the filtering 
            // triggered by the TextBox.TextChanged event ignore the changes 
            // introduced by this KeyDown event.
            // DispatcherPriority.ContextIdle will force to "override" this behavior.
            await Application.Current.Dispatcher.InvokeAsync(
              () =>
              {
                ComboBox.SelectCurrentItem(textBox, comboBox);
                ComboBox.IsNavigationKeyPressed = false;
              }, 
              DispatcherPriority.ContextIdle);
    
            break;
          }
        }
      }
    
      private static void SelectCurrentItem(TextBox textBox, System.Windows.Controls.ComboBox comboBox)
      {
        comboBox.SelectedItem = comboBox.Items.CurrentItem;
        if (ComboBox.TextBoxSelectionStartMap.TryGetValue(textBox, out int selectionStart))
        {
          textBox.SelectionStart = selectionStart;
        }
      }
    
      private static void EnableAutocompleteFilterOnComboBoxLoaded(object sender, RoutedEventArgs e)
      {
        var comboBox = sender as System.Windows.Controls.ComboBox;
        ComboBox.EnableAutocompleteFilter(comboBox);
      }
    
      private static void EnableAutocompleteFilter(System.Windows.Controls.ComboBox comboBox)
      {
        if (comboBox.TryFindVisualChildElement(out TextBox editTextBox))
        {
          ComboBox.TextBoxComboBoxMap.Add(editTextBox, comboBox);
          ComboBox.ComboBoxTextBoxMap.Add(comboBox, editTextBox);
          editTextBox.TextChanged += ComboBox.FilterOnTextInput;
    
          // Need to receive handled KeyDown event
          comboBox.AddHandler(UIElement.PreviewKeyDownEvent, new KeyEventHandler(HandleKeyDownWhileFiltering), true);
        }
      }
    
      private static void DisableAutocompleteFilter(System.Windows.Controls.ComboBox comboBox)
      {
        if (comboBox.TryFindVisualChildElement(out TextBox editTextBox))
        {
          ComboBox.TextBoxComboBoxMap.Remove(editTextBox);
          editTextBox.TextChanged -= ComboBox.FilterOnTextInput;
        }
      }
    }
    

    扩展.cs
    public static class Extensions
    { 
      /// <summary>
      /// Traverses the visual tree towards the leafs until an element with a matching element type is found.
      /// </summary>
      /// <typeparam name="TChild">The type the visual child must match.</typeparam>
      /// <param name="parent"></param>
      /// <param name="resultElement"></param>
      /// <returns></returns>
      public static bool TryFindVisualChildElement<TChild>(this DependencyObject parent, out TChild resultElement)
        where TChild : DependencyObject
      {
        resultElement = null;
    
        if (parent is Popup popup)
        {
          parent = popup.Child;
          if (parent == null)
          {
            return false;
          }
        }
    
        for (var childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); childIndex++)
        {
          DependencyObject childElement = VisualTreeHelper.GetChild(parent, childIndex);
          if (childElement is TChild child)
          {
            resultElement = child;
            return true;
          }
    
          if (childElement.TryFindVisualChildElement(out resultElement))
          {
            return true;
          }
        }
    
        return false;
      }
    }
    

    使用示例
    <ComboBox ItemsSource="{Binding Items}" 
              IsEditable="True"
              ComboBox.IsFilterOnAutocompleteEnabled="True" />
    

    关于c# - 具有过滤器和自动完成功能的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008226/

    相关文章:

    wpf - 在更新绑定(bind)源期间卡住 WPF 应用程序

    .net - MVVM 模式存在哪些问题?

    C# List Group By 和 where

    c# - 在 blob 存储中并行上传 block

    c# - WPF - 将 ComboBox 项目前景绑定(bind)到它的值

    wpf - MVVM 和 StructureMap 使用

    design-patterns - MVVM Light DI 在虚拟机之间共享数据?

    c# group by 不起作用

    c# - Windows Phone 8商店测试套件错误

    wpf - AvalonDock 与 MVVM,文档关闭不会删除 DocumentsSource 中的项目