Searching in .NET MAUI ComboBox (SfComboBox)
21 Jun 20245 minutes to read
The ComboBox control provides rich text searching functionality. The TextSearchMode
property of the ComboBox can be used to regulate how the control behaves when it receives user input.
Search based on member path
The DisplayMemberPath and TextMemberPath properties of ComboBox control specify the property path, by which the searching must be done when a custom data is bound to the ItemsSource property.
-
DisplayMemberPath
- Specifies the property path whose value is displayed as text in the drop-down menu. The default value isstring.Empty
. -
TextMemberPath
- Specifies the property path whose value is used to perform searching based on user input received in the selection box portion of theComboBox
control. The default value isstring.Empty
. WhenTextMemberPath
isnull
orstring.Empty
, searching will be performed based onDisplayMemberPath
.
NOTE
DisplayMemberPath
andTextMemberPath
will be effective for the collection item that holds two or more properties in it.
NOTE
When both the
DisplayMemberPath
andTextMemberPath
properties have anull
orstring.Empty
value, searching will be performed based on the class name with namespace of the item.
Edit mode Searching based on TextMemberPath
In edit mode, searching will be performed based on the TextMemberPath property while entering the text into the selection box. When TextMemberPath is null
or string.Empty
, searching will be performed based on DisplayMemberPath.
<editors:SfComboBox x:Name="comboBox"
WidthRequest="250"
IsEditable="true"
ItemsSource="{Binding SocialMedias}"
TextMemberPath="ID"
DisplayMemberPath="Name" />
comboBox.TextMemberPath = "ID";
The following image illustrates the result of the above code:
NOTE
Auto appending of text is supported only in
Editable
mode andTextSearchMode
property should beStartsWith
.
Text Search Mode
The TextSearchMode property of the ComboBox can be used to regulate how the control behaves when it receives user input. The default text searching type is StartWith, ignoring accent and it is case insensitive. The available text search modes are:
- StartsWith
- Contains
Search with beginning text
Set the TextSearchMode property value to StartWith to search the matching items based on the starting text, and the first item which fits the user input in the drop-down list, will be highlighted.
<editors:SfComboBox x:Name="comboBox"
WidthRequest="250"
TextSearchMode="StartsWith"
ItemsSource="{Binding SocialMedias}"
DisplayMemberPath="Name"
TextMemberPath="Name" />
comboBox.TextSearchMode = ComboBoxTextSearchMode.StartsWith;
The following image illustrates the result of the above code:
Search with contains text
Set the TextSearchMode property value to Contains to search the matching items containing specific text, and the first item which fits the user input in the drop-down list, will be highlighted.
<editors:SfComboBox x:Name="comboBox"
WidthRequest="250"
IsEditable="True"
TextSearchMode="Contains"
ItemsSource="{Binding SocialMedias}"
TextMemberPath="Name"
DisplayMemberPath="Name" />
comboBox.TextSearchMode = ComboBoxTextSearchMode.Contains;
The following image illustrates the result of the above code:
Prefix characters constraint
Instead of displaying suggestion list on every character entry, matches can be filtered and displayed after a few character entries. This can be done by MinimumPrefixCharacters property and its default value is 1.
<editors:SfComboBox x:Name="comboBox"
WidthRequest="300"
IsFilteringEnabled="True"
IsEditable="True"
ItemsSource="{Binding SocialMedias}"
MinimumPrefixCharacters="3"
DisplayMemberPath="Name"
TextMemberPath="Name">
comboBox.MinimumPrefixCharacters = 3;
The following image illustrates the result of the above code.