The users wanted an AutoComplete Combo box in a SilverLight application- a combo box that would allow them to scroll the entire list, as well as filter the list as the user typed in the box.
The problem is: SilverLight has no such control. I did a bit of research and came across various options using templates, styles, etc.
Try as I might, I could not get these samples to work as I needed it to. Finally, in desperation, hours before the deadline, I tried the following solution. In about 15 minutes I had a control that functioned exactly as the users wanted.
It's not elegant- it's so simple that I laughed out loud. the solution is below.
The page already had a ComboBox on it. The task was to add the ability to type and filter the list.
First, Download the SilverLight Tool kit, and Reference
System.Windows.Controls.Input
in your application.
Add the reference to the xaml (I used "toolkit")
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input"
Below is the original ComboBox:
<ComboBox
x:Name="cbAffiliate"
Grid.Column="1" Grid.Row="1" Grid.RowSpan="1"
Margin="2,1,2,1" FontSize="9" HorizontalAlignment="Left" Height="20"
ItemsSource="{Binding Mode=OneWay, Path=MediaCodeList}"
SelectedItem="{
Binding Mode=TwoWay, Path=SelectedMediaCode}"
SelectionChanged="cbAffiliate_SelectionChanged" MinWidth="200" >
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel MinHeight="300"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel
>
</ComboBox>
Became:
<Grid Grid.Column="1" Grid.Row="1" Grid.RowSpan="1">
<ComboBox
x:Name="cbAffiliate"
Margin="2,1,2,1" FontSize="9" HorizontalAlignment="Left" Height="20"
ItemsSource="{Binding Mode=OneWay, Path=MediaCodeList}"
SelectedItem="{
Binding Mode=TwoWay, Path=SelectedMediaCode}"
SelectionChanged="cbAffiliate_SelectionChanged" MinWidth="200" >
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel MinHeight="300"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel
>
</ComboBox>
<toolkit:AutoCompleteBox
x:Name="txtAffiliate"
Margin="2,1,2,1" FontSize="9" HorizontalAlignment="Left" Height="20"
ItemsSource="{Binding Mode=OneWay, Path=MediaCodeList}"
SelectedItem="{
Binding Mode=TwoWay, Path=SelectedMediaCode}"
SelectionChanged="cbAffiliate_SelectionChanged" MinWidth="180" >
</toolkit:AutoCompleteBox>
</Grid>
Basically I:
1) Enclosed the ComboBox in a new Grid and moved the
Grid.Column, Grid.Row, Grid.RowSpan attributes up to that level.
2) Added an AutoCompleteBox right on top of the ComboBox, and set all the attributes the same as the original ComboBox
3) Made the AutoCompleteBox 20 pixels shorter than the ComboBox. (The MinWidth attribute).
It works just as the users wanted!