This article has the goal to show how to use the AutoCompleteBox for Universal Apps, from Telerik.
Some time ago Telerik published the UI For Windows Universal as a Beta version, which can be download in Telerik WebSite.
For now is only available the following controls:
In this article we will focus in the AutoCompleteBox. Let's see how we can use it!
Before download and install UI For Windows Universal, we should create a Blank Universal App, then we need to add the reference for this. The following image show how to add it:
Now we can start use the Telerik controls, but before we can move MainWindow.xaml and App.xaml for the shared project for reuse the code.
Note: In this article we will use MVVMLight, see more about here.
In MainWindow we can define the UI as following
<
Page
x:Class
=
"MyTelerikSamples.AutoCompleteBoxSample.MainPage"
xmlns
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
"http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
"http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
"http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:input
"using:Telerik.Universal.UI.Xaml.Controls.Input"
mc:Ignorable
"d"
DataContext
"{Binding Main, Source={StaticResource Locator}}"
Background
"{ThemeResource ApplicationPageBackgroundThemeBrush}"
>
Grid
Grid.ColumnDefinitions
ColumnDefinition
Width
"{StaticResource HeaderWidth}"
/>
"*"
</
Grid.RowDefinitions
RowDefinition
Height
"{StaticResource HeaderHeigth}"
TextBlock
Grid.Row
"0"
Grid.Column
"1"
VerticalAlignment
"Center"
Style
"{StaticResource HeaderTextBlockStyle}"
Grid.ColumnSpan
"2"
>RadAutoCompleteBox Sample</
StackPanel
Margin
"20,10,0,0"
Orientation
"Vertical"
FontSize
"20"
>Insert some text:</
input:RadAutoCompleteBox
"0,20,0,0"
FilterMode
"Contains"
IsTextMatchHighlightEnabled
"True"
Text
"{Binding Value, Mode=TwoWay}"
FilterComparisonMode
"CurrentCultureIgnoreCase"
ItemsSource
"{Binding Suggestions}"
AutosuggestFirstItem
"False"
"200"
HorizontalAlignment
"Left"
input:RadAutoCompleteBox.ItemTemplate
DataTemplate
"{Binding}"
input:RadAutoCompleteBox.IsTextMatchHighlightEnabled
input:RadAutoCompleteBox.TextMatchHighlightStyle
input:HighlightStyle
Foreground
"DeepPink"
"21"
In AutoCompleteBox we defined a binding for the Text, which define the text string we will write in the control and we defined the ItemsSource which are the suggestion we will provide to the control, with this each time we start to write a word the control will try to find available words that match with the user input.
The DataTemplate was defined for allow to highlight the matched chars when we insert the text, in this case will be highlight by Pink color.
The MainViewModel can be defined as following
public
class
MainViewModel : ViewModelBase
{
private
string
_value;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
Value
get
return
_value; }
set
{ Set(() => Value,
ref
_value, value); }
}
/// Gets the suggestions.
/// <value>The suggestions.</value>
IEnumerable<
> Suggestions
new
List<
"Apple"
,
"Babaco"
"Bacupari"
"Bacuri"
"Black cherry"
"Pineapples"
"Orange"
"Tomato"
};
SolidColorBrush
x:Key
"TelerikAutoCompleteBoxSelectorSelectedItemBackgroundBrush"
Color
"TelerikAutoCompleteBoxFocusedForegroundBrush"
"White"
Application
"MyTelerikSamples.AutoCompleteBoxSample.App"
xmlns:viewModel
"using:MyTelerikSamples.AutoCompleteBoxSample.ViewModel"
xmlns:controls
"using:Telerik.Universal.UI.Xaml.Controls"
Application.Resources
ResourceDictionary
viewModel:ViewModelLocator
"Locator"
controls:UserThemeResources
"ThemeResource"
DarkResourcesPath
"ms-appx:///Styles/TelerikResources.xaml"
LightResourcesPath
ResourceDictionary.ThemeDictionaries
"Default"
ResourceDictionary.MergedDictionaries
<!-- your namespace is "Telerik.Universal.UI.Xaml.Input" -->
Source
"ms-appx:///Telerik.Universal.UI.Xaml.Input/Themes/ThemeResourcesDark.xaml"
"{CustomResource DarkResourcesPath}"
"Light"
"ms-appx:///Telerik.Universal.UI.Xaml.Input/Themes/ThemeResourcesLight.xaml"
"{CustomResource LightResourcesPath}"
"/Style/Definition.xaml"
When we run the app, we will get
The source code is available in github.
Telerik UI for Windows 8 XAML Documentation - Telerik Named Brushes
Telerik UI for Windows 8 XAML Documentation - Resolving Telerik named resources