有没有办法在telerik:RadAutoCompleteBox中多次选择一项?

时间:2019-01-17 16:20:57

标签: c# xaml autocomplete telerik

我正在尝试制作类似控件的“代码编辑器”。我想要已知变量和运算符的自动完成功能。 RadAutoCompleteBox几乎可以完全满足我的需要,它只是从列表中删除选定的项目,因此我只能对每个变量或运算符使用一次。我可以禁用此功能吗?

如上所述,我在SelectionMode =“ Multiple”中将RadAutoCompleteBox与所有已知变量和运算符的列表的ItemSource一起使用。我只是找不到启用多选多选的选项。

XAML:

<telerik:GridViewDataColumn.CellEditTemplate>
<DataTemplate>
<telerik:RadAutoCompleteBox SelectedItem="{Binding RecipeCondition, Mode=TwoWay}" ItemsSource="{Binding DataContext.ConditionWordList, RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}" SelectionMode="Multiple" AutoCompleteMode="SuggestAppend" TextSearchMode="Contains">
</telerik:GridViewDataColumn.CellEditTemplate>

ViewModel:

 private void initFakeData()
    {
      string[] operators = { " AND ", " OR ", " NOT ", " + " , " - ", " * ", " / ", "=", "!=", ">", "<", ">=", "<=" };
      string[] vars = { "{VAR_1}", "{VAR_2}", "{OTHER_VAR}", "{sumDamnVar}", "{121asdf}", "{121onji}", "{12123}"};
      foreach(string oupii in operators)
      {
        ConditionWordList.Add(oupii);
      }

      foreach (string variable in vars)
      {
        ConditionWordList.Add(variable);
      }
    }
public ObservableCollection<EventWrapper> EventMappingList { get; set; } = new ObservableCollection<EventWrapper>();
public ObservableCollection<string> ConditionWordList{ get; set; } = new ObservableCollection<string>();

即使我键入了“ {VAR_1} AND {VAR_2}”,我仍希望结果仍然提示“ AND”运算符

1 个答案:

答案 0 :(得分:0)

因此,经过一天的广泛研究,我找到了一个简单的解决方案:创建自定义过滤条件。

class ComboBoxLikeFilter : FilteringBehavior, IFilteringBehavior
  {
    public override IEnumerable<object> FindMatchingItems(string searchText, IList items, IEnumerable<object> escapedItems, string textSearchPath, TextSearchMode textSearchMode)
    {
      if (string.IsNullOrWhiteSpace(searchText))
      {
        return ((IEnumerable<object>)items);//.Where(x => !escapedItems.Contains(x));
      }
      return base.FindMatchingItems(searchText, items, new object[0], textSearchPath, textSearchMode) as IEnumerable<object>;
    }
  }

并将其连接到您的AutoCompleteBox:

<telerik:GridViewDataColumn.CellEditTemplate>
<DataTemplate>
<telerik:RadAutoCompleteBox SelectedItem="{Binding RecipeCondition, Mode=TwoWay}" ItemsSource="{Binding DataContext.ConditionWordList, RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}" SelectionMode="Multiple" AutoCompleteMode="SuggestAppend" TextSearchMode="Contains">
<telerik:RadAutoCompleteBox.FilteringBehavior>
<behavior:ComboBoxLikeFilter/>
</telerik:RadAutoCompleteBox.FilteringBehavior>
</telerik:RadAutoCompleteBox>
</telerik:GridViewDataColumn.CellEditTemplate>

这使您可以多次选择项目并获得ObservableCollection。完成后,看起来就不正确了(显然telerik只是调用了'ToString'方法来显示列表),所以我做了自己的

class StringList : ObservableCollection<string>
  {
    public override string ToString()
    {
      if(this.Count == 0)
      {
        return string.Empty;
      }
      return string.Join(" ", this);
    }
  }

希望这对某人有帮助,因为那只花了我大约2天的时间来弄清楚。