As a RadComboBox user you probably are already aware of the fact that it provides two edit modes out of the box – along with the default non-editable mode there is an editable one. The editable mode allows the user to get the desired item by typing, however it also ignores the SelectionBoxTemplate which gives you the ability to display the selected item differently.
In many scenarios you might need to use both - the editable functionality and the possibility to modify the appearance of the selected item in order to visualize two or more properties of the selected custom object. This article will demonstrate how this scenario can be easily achieved by modifying the EditableTemplate of RadComboBox.
For the example we will need a custom object named Company and a collection of Company objects in the ViewModel to be bound to the ItemsSource of the ComboBox.
The Company class contains the following set of properties and constructor:
public
class
Company
{
public
Company(
string
name,
string
address,
string
phone,
string
image)
{
this
.Name = name;
this
.Address = address;
this
.Phone = phone;
this
.Image = image;
}
public
string
Name {
get
;
set
;}
public
string
Phone {
get
;
set
; }
public
string
Image {
get
;
set
; }
public
string
Address {
get
;
set
; }
}
And the ViewModel should look as shown below:
public
class
CompaniesViewModel
{
private
ObservableCollection<Company> companies;
public
ObservableCollection<Company> Companies
{
get
{
if
(companies ==
null
)
{
companies =
new
ObservableCollection<Company>();
companies.Add(
new
Company(
"Globex Corporation"
,
"France, Marseille"
,
"(100) 555-4822"
,
"Images/Image3.png"
));
companies.Add(
new
Company(
"Atlantic Northern"
,
"Brazil, São Paulo"
,
"(11) 555-1189"
,
"Images/Image5.png"
));
companies.Add(
new
Company(
"Roboto Industries"
,
"Spain, Madrid"
,
"(91) 745 6200"
,
"Images/Image6.png"
));
companies.Add(
new
Company(
"Galaxy Corp"
,
"Sweden, Luleå"
,
"0921-12 34 65"
,
"Images/Image7.png"
));
companies.Add(
new
Company(
"Wayne Enterprises"
,
"USA, Portland"
,
"(503) 555-3612"
,
"Images/Image8.png"
));
companies.Add(
new
Company(
"Acme, inc."
,
"Finland, Helsinki"
,
"90-224 8858"
,
"Images/Image9.png"
));
companies.Add(
new
Company(
"Consolidated Holdings"
,
"UK, London"
,
"(171) 555-2282"
,
"Images/Image4.png"
));
}
return
companies;
}
}
}
So far so good! Now we can include the following ComboBox definition in XAML (don’t forget to set the IsEditable property to True):
<
telerik:RadComboBox
x:Name
=
"comboBox"
IsEditable
=
"True"
ItemsSource
=
"{Binding Companies}"
DisplayMemberPath
=
"Name"
Width
=
"200"
Height
=
"50"
/>
By now the ComboBox will look as shown below, nothing special:
Next step would be to extract the default EditableTemplate of RadCombBox. You can find it in the Telerik.Windows.Controls.Input.xaml file which is located in the Themes.Implicit folder inside of the installation folder of the controls. You will need to extract the Template for the same Theme you are using in the project – in our case this is the Windows8Theme. Afterwards you would only need to merge it in App.xaml along with the theme ResourceDictionaries using Implicit Styles:
<
Application.Resources
>
<
ResourceDictionary
>
<
ResourceDictionary.MergedDictionaries
>
<
ResourceDictionary
Source
=
"/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml"
/>
<
ResourceDictionary
Source
=
"/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml"
/>
<
ResourceDictionary
Source
=
"/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml"
/>
<
ResourceDictionary
>
<
ControlTemplate
x:Key
=
"EditableComboBox"
TargetType
=
"telerik:RadComboBox"
>
...
</
ControlTemplate
>
</
ResourceDictionary
>
</
ResourceDictionary.MergedDictionaries
>
</
ResourceDictionary
>
</
Application.Resources
>
Great, now since the required Template is present the only thing has left is to modify it the required way. What we need to do is to find the TextBox named PART_EditableTextBox inside of the EditableTemplate and modify its Template in order to include an additional StackPanel below the ScrollViewer there. The StackPanel will contain three elements – Image and two TextBlocks bound to the desired properties of the SelectedItem. In our scenario we will need to display the Image, the Address and the Phone properties of the Company. The following snippet illustrates how to set up all of elements correctly:
<
StackPanel
Orientation
=
"Horizontal"
Height
=
"22"
VerticalAlignment
=
"Bottom"
Grid.Row
=
"1"
Background
=
"{TemplateBinding Background}"
>
<
Image
Width
=
"14"
Height
=
"12"
Source
=
"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadComboBox}, Path=SelectedItem.Image}"
Margin
=
"{TemplateBinding Padding}"
/>
<
TextBlock
VerticalAlignment
=
"Center"
FontSize
=
"12"
IsHitTestVisible
=
"False"
Text
=
"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadComboBox}, Path=SelectedItem.Address}"
/>
<
TextBlock
Padding
=
"2 0 0 0"
VerticalAlignment
=
"Center"
FontSize
=
"12"
IsHitTestVisible
=
"False"
Text
=
"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadComboBox}, Path=SelectedItem.Phone}"
/>
</
StackPanel
>
Also to be able to hide the additional elements whenever there is nothing selected, we will need to toggle the Visibility of the same StackPanel by binding it to the ComboBox SelectedItem using a simple null to visibility converter:
<
StackPanel
Visibility="{Binding RelativeSource={RelativeSource
Mode
=
FindAncestor
,
AncestorType
=
telerik
:RadComboBox},
Path
=
SelectedItem
,
Converter={StaticResource NullToVisibilityConverter}}" … />
Almost there – the final step after the Template is modified as required would be to apply it to the ComboBox the following way:
<
telerik:RadComboBox
EditableTemplate
=
"{StaticResource EditableComboBox}"
… />
And voilà, here is the result:
That’s it! Now you have editable ComboBox that displays a couple more properties at the same time. You can download a runnable project of the same example from our online SDK repository here, the example is listed as ComboBox / EditableTextBoxTemplate. Make sure to check it out and play around with the source!
Even more with the next major release of the UI for Silverlight/WPF RadComboBox will provide a brand new TextBoxStyle property which will allow you to easily customize the editable ComboBox in various other ways.
Stay tuned for more XAML customization tips and tricks and if you have any thoughts to share, do not hesitate to leave a comment below!