WPF combobox textsearch contains

How to implement your Combobox TextSearch using contains instead of StartsWith

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

The search function works, but I need to match the substrings

+4
source share
5 answers

Here I have an example within the framework of MVVM.

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 anchored to the string (SearchText). As soon as the TextChanged event occurs, a drop-down list is displayed, and two-way binding updates the value. ItemsSource has changed in the cs file when it is included in the set {}; syntax.

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

+4
source

Cannot replace string.StartsWith () with string.Contains (). You must write your own ComboBox.

This article can help you: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S

+2
source

This example looks like "TextSearch"

In the XAML file, you should add only one attribute to the combobox "TextContainSearch.Text":

 <ComboBox ItemsSource="{Binding Model.formListIntDeviceNumbers}" SelectedItem="{Binding Path=Model.selectedDeviceNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DeviceNumber" IsEditable="True" c:TextContainSearch.Text="DeviceNumber"> 

And we must add use to the XAML file header:

 xmlns:c="clr-namespace:Adaptive.Controls.Extension" 

And the C # code in the * .cs file:

 using System; using System.Windows; using System.Windows.Controls; namespace Adaptive.Controls.Extension { public sealed class TextContainSearch : DependencyObject { public static void SetText(DependencyObject element, string text) { var controlSearch = element as Control; if (controlSearch != null) controlSearch.KeyUp += (sender, e) => { if (sender is ComboBox){ var control = sender as ComboBox; control.IsDropDownOpen = true; var oldText = control.Text; foreach(var itemFromSource in control.ItemsSource){ if (itemFromSource != null) { Object simpleType = itemFromSource.GetType().GetProperty(text).GetValue(itemFromSource, null); String propertOfList = simpleType as string; if (!string.IsNullOrEmpty(propertOfList) && propertOfList.Contains(control.Text)) { control.SelectedItem = itemFromSource; control.Items.MoveCurrentTo(itemFromSource); break; } } } control.Text = oldText; TextBox txt = control.Template.FindName("PART_EditableTextBox", control) as TextBox; if (txt != null) { txt.Select(txt.Text.Length, 0); } } }; } } } 
+1
source

Try it:

  <ComboBox Padding="3,5" MinWidth="150" SelectedItem="{Binding NewBoxRequest}" ItemsSource="{Binding Requests}" DisplayMemberPath="SN" IsEditable="True" StaysOpenOnEdit="True" Text="{Binding SnFilter,UpdateSourceTrigger=PropertyChanged}"> </ComboBox> 

view model:

  private string snFilter; public string SnFilter { get { return snFilter; } set { snFilter = value; RaisePropertyChanged(); RaisePropertyChanged(nameof(Requests)); } } private List<Request> requests; public List<Request> Requests { get => string.IsNullOrEmpty(SnFilter) || requests.Any(r => r.SN == SnFilter) ? requests : requests.Where(r => r.SN.Contains(SnFilter)).ToList(); set { requests = value; RaisePropertyChanged(); } } 
0
source

I could not get the Install syntax to work on my C # system, so the following is a small answer to Wu's question (this is in a user control):

  <ComboBox IsEditable="True" IsTextSearchEnabled="false" ItemsSource="{Binding pData, RelativeSource = {RelativeSource TemplatedParent}}" DisplayMemberPath="description" Text="{Binding SearchText , RelativeSource = {RelativeSource TemplatedParent}, 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> 

In user control:

 private async void _Loaded(object sender, RoutedEventArgs e) { var n = await InitializeLabTests; allTests = new ObservableCollection<CommonProcedure>(n); pData = new ObservableCollection<CommonProcedure>(n); } //ItemsSource - pData //There is a string attribute - wTitle included in the fooClass (DisplayMemberPath) private ObservableCollection<CommonProcedure> __pData; public ObservableCollection<CommonProcedure> pData { get { return __pData; } set { __pData = value; RaisePropertyChanged(); } } private string _SearchText; public string SearchText { get { return _SearchText; } set { _SearchText = value; RaisePropertyChanged(); //Update your ItemsSource here with Linq pData = new ObservableCollection<CommonProcedure> ( allTests.Where(q => q.description.Contains(SearchText)) ); } } 

The only significant difference is the SearchText installer.

0
source

Source: https://habr.com/ru/post/1469433/


All Articles