WPF组合框textsearch与包含

JMa*_*Man 4 wpf search combobox

我如何使用contains而不是实现Combobox TextSearchStartsWith

<rf:ComboBox Grid.Row="1"
                         Grid.Column="5"
                         Width="200"
                         ItemsSource="{Binding Source={StaticResource AccountProvider}}"
                         DisplayMemberPath="Description"
                         SelectedValuePath="IndRekId"
                         IsEmptyItemVisible="True"
                         SelectedValue="{Binding Id, UpdateSourceTrigger=PropertyChanged}"
                         IsTextSearchEnabled="True"
                         TextSearch.TextPath="Description"
                         IsEditable="True"/>
Run Code Online (Sandbox Code Playgroud)

搜索功能有效,但我需要在子字符串上进行匹配

Ton*_* Wu 5

在这里,我有一个MVVM框架的示例。

我的xaml文件:

<ComboBox Name="cmbContains" IsEditable="True" IsTextSearchEnabled="false" ItemsSource="{Binding pData}"  DisplayMemberPath="wTitle" Text="{Binding SearchText ,Mode=TwoWay}"  >
  <ComboBox.Triggers>
      <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
          <BeginStoryboard>
              <Storyboard>
                  <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                      <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                  </BooleanAnimationUsingKeyFrames>
              </Storyboard>
          </BeginStoryboard>
      </EventTrigger>
  </ComboBox.Triggers>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

我的cs文件:

//ItemsSource - pData
//There is a string attribute - wTitle included in the fooClass (DisplayMemberPath)
private ObservableCollection<fooClass> __pData;
public ObservableCollection<fooClass> pData {
    get { return __pData; }
    set { Set(() => pData, ref __pData, value);
        RaisePropertyChanged("pData");
    }
}

private string _SearchText;
public string SearchText {
    get { return this._SearchText; }
    set {
        this._SearchText = value;
        RaisePropertyChanged("SearchText");

        //Update your ItemsSource here with Linq
        pData = new ObservableCollection<fooClass>{pData.ToList().Where(.....)};
    }
}
Run Code Online (Sandbox Code Playgroud)

您会看到可编辑的comboBox绑定到字符串(SearchText),一旦出现TextChanged事件,就会显示下拉列表,并且双向绑定会更新该值。进入set {}时,cs文件中的ItemsSource更改了;句法。

https://gist.github.com/tonywump/82e66abaf71f715c4bd45a82fce14d80


小智 2

无法将 string.StartsWith() 替换为 string.Contains()。您必须编写自定义组合框。

本文可能对您有帮助: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S