WPF ComboBox TextSearch using Contains instead of StartsWith

I got a ComboBox with a lot of "Clients" using MultiBinding as Text (for example, "644 Pizza Place"), and this works great from the very beginning (CustomerNumber). But how can I make this a coincidence and choose just enter "Pizza Place"?

 <MultiBinding StringFormat="{}{0} {1}"> <Binding Path="CustomerNumber" /> <Binding Path="CustomerName" /> </MultiBinding> 
+6
source share
2 answers

ComboBox uses the TextSearch class to search for an item. You can set the dependency property of TextSearch.TextPath in a ComboBox:

  <ComboBox Name="cbCustomers" TextSearch.TextPath="CustomerName">...</ComboBox> 

This will allow you to match the CustomerName name, but you will lose the match by the Customer number.

A search without special details is performed as follows: The ComboBox.TextUpdated method is called upon entry. This method calls TextSearch.FindMatchingPrefix to find the corresponding item. TextSearch.FindMatchingPrefix is โ€‹โ€‹a method that uses calls to string.StartsWith (..).

There is no way to replace calls to string.StartsWith () or TextSearch.FindMatchingPrefix calling something else. So it looks like you need to write your own ComboBox class if you want to swap the .StartsWith () line with your custom logic (e.g. string.Contains)

+4
source

Here I have alternatives in the MVVM framework.

my xaml file:

 <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> 

My cs file:

 //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(.....)}; } } 

You can see that the comboBox being edited is a string binding (SearchText) After the TextChanged event occurs, a drop-down menu is displayed and the two-way binding updates the value. The ItemsSource element is changed in the cs file when it is included in the set {}; syntax.

The bottom line with the code above

0
source

All Articles