Telerik: AutoCompleteBox For Universal Apps

Scope

The purpose of this article is to show how to use the AutoCompleteBox for Universal Apps, from Telerik.

Introduction

Some time ago Telerik published the UI For Windows Universal as a Beta version, that can be downloaded from the Telerik WebSite.

windows universal

For now only the following controls are available:

  • AutoCompleteBox
  • BusyIndicator
  • Chart
  • DataBoundListBox
  • DatePicker
  • Rating
  • TimePicker

This article focuses on the AutoCompleteBox. Let's see how to use it!

Description

Before downloading and installing UI For Windows Universal, we should create a Blank Universal App, then we need to add the reference for it. The following image shows how to add it:

add reference

And then:

extensions

Now we can start using the Telerik controls, but before we can move MainWindow.xaml and App.xaml for the shared project to reuse the code.

Note: In this article we will use MVVMLight, see more about that here.

In MainWindow we can define the UI as in the following:

  1. <Page  
  2.     x:Class="MyTelerikSamples.AutoCompleteBoxSample.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  7.     xmlns:input="using:Telerik.Universal.UI.Xaml.Controls.Input"  
  8.     mc:Ignorable="d"  
  9.     DataContext="{Binding Main, Source={StaticResource Locator}}"  
  10.     Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11.     <Grid>  
  12.         <Grid.ColumnDefinitions>  
  13.             <ColumnDefinition Width="{StaticResource HeaderWidth}"/>  
  14.             <ColumnDefinition Width="*"/>  
  15.         </Grid.ColumnDefinitions>  
  16.         <Grid.RowDefinitions>  
  17.             <RowDefinition Height="{StaticResource HeaderHeigth}"/>  
  18.             <RowDefinition Height="*"/>  
  19.         </Grid.RowDefinitions>  
  20.         <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Style="{StaticResource HeaderTextBlockStyle}" Grid.ColumnSpan="2">RadAutoCompleteBox Sample</TextBlock>  
  21.         <StackPanel Margin="20,10,0,0" Orientation="Vertical"  Grid.Row="1" Grid.Column="1">  
  22.             <TextBlock  FontSize="20">Insert some text:</TextBlock>  
  23.             <input:RadAutoCompleteBox Margin="0,20,0,0"  
  24.                      FilterMode="Contains" IsTextMatchHighlightEnabled="True"  
  25.                      Text="{Binding Value, Mode=TwoWay}"   
  26.                      FilterComparisonMode="CurrentCultureIgnoreCase"  
  27.                      ItemsSource="{Binding Suggestions}" AutosuggestFirstItem="False"  
  28.                      Width="200" HorizontalAlignment="Left">  
  29.                 <input:RadAutoCompleteBox.ItemTemplate>  
  30.                     <DataTemplate>  
  31.                         <TextBlock Text="{Binding}"  
  32.                                input:RadAutoCompleteBox.IsTextMatchHighlightEnabled="True">  
  33.                           <input:RadAutoCompleteBox.TextMatchHighlightStyle>  
  34.                               <input:HighlightStyle Foreground="DeepPink" FontSize="21"/>  
  35.                           </input:RadAutoCompleteBox.TextMatchHighlightStyle>  
  36.                         </TextBlock>  
  37.                     </DataTemplate>  
  38.                 </input:RadAutoCompleteBox.ItemTemplate>  
  39.             </input:RadAutoCompleteBox>  
  40.         </StackPanel>  
  41.     </Grid>  
  42. </Page>  
In AutoCompleteBox we defined a binding for the Text that defines the text string we will write to the control and we defined the ItemsSource that is the suggestion we will provide to the control, with this each time we start to write a word the control will try to find the available words that match with the user input.

The DataTemplate was defined to allow to highlight the matched chars when we insert the text, in this case it will be highlighted by the Pink color.

The MainViewModel can be defined as in the following:
  1. public class MainViewModel : ViewModelBase  
  2.     {  
  3.         private string _value;  
  4.   
  5.         /// <summary>  
  6.         /// Gets or sets the value.  
  7.         /// </summary>  
  8.         /// <value>  
  9.         /// The value.  
  10.         /// </value>  
  11.         public string Value  
  12.         {  
  13.             get { return _value; }  
  14.             set { Set(() => Value, ref _value, value); }  
  15.         }  
  16.   
  17.         /// <summary>  
  18.         /// Gets the suggestions.  
  19.         /// </summary>  
  20.         /// <value>The suggestions.</value>  
  21.         public IEnumerable<string> Suggestions  
  22.         {  
  23.             get  
  24.             {  
  25.                 return new List<string>  
  26.                 {  
  27.                      "Apple",  
  28.                      "Babaco",  
  29.                      "Bacupari",  
  30.                      "Bacuri",  
  31.                      "Black cherry",  
  32.                      "Pineapples",  
  33.                      "Orange",  
  34.                      "Tomato",  
  35.                 };  
  36.             }  
  37.         }  
  38.     }  
We can now run the app to test if the behavior does what we want, but before we should change the style for the selected item, because it will show the default colors and Pink was choosen for the highlight. For it, create a dictionary to add the resource we want to override.
  1. <SolidColorBrush x:Key="TelerikAutoCompleteBoxSelectorSelectedItemBackgroundBrush" Color="DeepPink"/>  
  2. <SolidColorBrush x:Key="TelerikAutoCompleteBoxFocusedForegroundBrush" Color="White"/>  
Then in App.xaml add the resources and the themes, as in the following:
  1. <Application  
  2.     x:Class="MyTelerikSamples.AutoCompleteBoxSample.App"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:viewModel="using:MyTelerikSamples.AutoCompleteBoxSample.ViewModel"  
  6.     xmlns:controls="using:Telerik.Universal.UI.Xaml.Controls">  
  7.     <Application.Resources>  
  8.         <ResourceDictionary>  
  9.             <viewModel:ViewModelLocator x:Key="Locator"/>  
  10.             <controls:UserThemeResources x:Key="ThemeResource"   
  11.                                          DarkResourcesPath="ms-appx:///Styles/TelerikResources.xaml"  
  12.                                          LightResourcesPath="ms-appx:///Styles/TelerikResources.xaml"/>  
  13.             <ResourceDictionary.ThemeDictionaries>  
  14.                 <ResourceDictionary x:Key="Default">  
  15.                     <ResourceDictionary.MergedDictionaries>  
  16.                         <!-- your namespace is "Telerik.Universal.UI.Xaml.Input" -->  
  17.                         <ResourceDictionary Source="ms-appx:///Telerik.Universal.UI.Xaml.Input/Themes/ThemeResourcesDark.xaml"/>  
  18.                         <ResourceDictionary Source="{CustomResource DarkResourcesPath}"/>  
  19.                     </ResourceDictionary.MergedDictionaries>  
  20.                 </ResourceDictionary>  
  21.                 <ResourceDictionary x:Key="Light">  
  22.                     <ResourceDictionary.MergedDictionaries>  
  23.                         <ResourceDictionary Source="ms-appx:///Telerik.Universal.UI.Xaml.Input/Themes/ThemeResourcesLight.xaml"/>  
  24.                         <ResourceDictionary Source="{CustomResource LightResourcesPath}"/>  
  25.                     </ResourceDictionary.MergedDictionaries>  
  26.                 </ResourceDictionary>  
  27.             </ResourceDictionary.ThemeDictionaries>  
  28.             <ResourceDictionary.MergedDictionaries>  
  29.                 <ResourceDictionary Source="/Style/Definition.xaml"/>  
  30.             </ResourceDictionary.MergedDictionaries>  
  31.         </ResourceDictionary>  
  32.     </Application.Resources>  
  33. </Application>  
Running the app.

When we run the app, we will get:

Running the app
radautocompletbox sample

Source Code

The source code is available in github.

See Also

Telerik UI for Windows 8 XAML Documentation - Telerik Named Brushes.

Telerik UI for Windows 8 XAML Documentation - Resolving Telerik named resources.

 

Up Next
    Ebook Download
    View all
    Learn
    View all