A common search functionality when using any search box is the highlighting of the matched text in the filtered items. For example if we are searching for an item “Example 1” and we type in “exam” we would want to have the items filtered and in the same time the matched text in those matched items bolded (like this “Example 1”). Achieving the described behavior is possible with the use of the RadAutoCompleteBox and its DropDownItemTemplate. The easiest approach we could use is by creating a custom control that will highlight the matched items text and simply place that control in the DropDownItemTemplate of the AutoCompleteBox.
The idea behind this custom control is that it will use the SearchText property to listen for the input text in the RadAutoCompleteBox control and updated the displayed text that will be visible in drop down portion of the control. Let’s see how this could be achieved:
public
class
TextBlockWithHighlight : Control
{
public
static
readonly
DependencyProperty SearchTextProperty =
DependencyProperty.Register(
"SearchText"
,
typeof
(
string
),
typeof
(TextBlockWithHighlight),
new
System.Windows.PropertyMetadata(
string
.Empty, OnSearchTextPropertyChanged));
public
static
readonly
DependencyProperty ItemTextProperty =
DependencyProperty.Register(
"ItemText"
,
typeof
(
string
),
typeof
(TextBlockWithHighlight),
new
System.Windows.PropertyMetadata(
string
.Empty, OnItemTextPropertyChanged));
private
string
searchText;
private
string
itemText;
public
string
SearchText
{
...
}
public
string
ItemText
{
...
}
}
public
override
void
OnApplyTemplate()
{
base
.OnApplyTemplate();
this
.textBoxPart =
this
.GetTemplateChild(
"PART_TextBlock"
)
as
TextBlock;
this
.BuildText();
}
After declaring the RadAutoCompleteBox and populating it with data you will need set our new TextBlockWithHighlight control to the DropDownItemTemplate:
<
telerik:RadAutoCompleteBox
...>
<
telerik:RadAutoCompleteBox.DropDownItemTemplate
>
<
DataTemplate
>
<
Grid
>
<
local:TextBlockWithHighlight
SearchText
=
"{Binding ElementName=AutoComplete, Path=SearchText}"
ItemText
=
"{Binding Name}"
/>
</
Grid
>
</
DataTemplate
>
</
telerik:RadAutoCompleteBox.DropDownItemTemplate
>
</
telerik:RadAutoCompleteBox
>
The next screenshot shows the final result in which a search for the latter “a” was made. Notice how the letter “a” is bolded in each matched item.
You can find runnable projects for both Silverlight and WPF in the XAML-SDK repository here. The examples are listed as AutoCompleteBox / HighlightMatchingItemsText. You can download a zip file of the entire XAML-SDK repository by clicking here.
The previously described case could be customized to fit different result populating mechanisms. If you have a good idea or a more case specific scenario that is interesting feel free to write a comment about it to this blog post.
Vladimir Amiorkov is a Software Developer at Progress. He is currently working with Web, iOS and Android technologies and is a part of the NativeScript team. In his spare time, he enjoys playing computer games such as Diablo and StarCraft.