Sometimes I think reading materials get overlooked in SDKs and we miss some hidden gems. One such gem I’d like to bring to your attention is the ability to add some subtle styling to an AutoCompleteBox from the Silverlight Toolkit to provide you with a cheap version of an editable ComboBox.
Sure Silverlight 2 has a ComboBox as part of the core controls now, but as I’ve previously noted, the ComboBox in current form exhibits only DropDownList behaviors. I’m sure this will likely change with future versions, but if you have a need for an editable ComboBox feel, here’s a start.
If you think about the AutoCompleteBox functionality, it provides a list of items that are filtered when the user starts typing in there. Well, that’s one function of a ComboBox right? The other exhibited behavior we need is to be able to click to activate the DropDownList functionality and select. Here’s where styling comes in (with a little help from some properties on AutoCompleteBox as well).
Let’s start by adding the items we’ll be dealing with: AutoCompleteBox and a data class (I’ll use a local hard-coded data class for portability in this code). To use the AutoCompleteBox, make sure you’ve downloaded the Silverlight Toolkit and then add a reference in your Silverlight project to Microsoft.Windows.Controls.dll. Then add the xmlns decoration in your UserControl…here’s mine (I use “toolkit”):
Here’s the Fruit class I’ll be using:
As you can see it is simple, but I wanted to make sure I was using more than just a string (yes I know essentially it is an object with only a string, but think outside the box :-)). Now we can wire up this code here to get our AutoCompleteBox behavior working:
Here we are using the same ItemFilter method I noted previously about AutoCompleteBox. Once we have this when we start typing we’ll get:
Now all we need to do is make it look and act also like a ComboBox. Here we can take styles and apply them. We’re going to do three things using Style resources:
- Modify the control template for our toolkit:AutoCompleteBox to include a ToggleButton
- Create a style for the ToggleButton that looks like an arrow to cue the user what to do with it
- Style the list displayed a little bit
Best of all – we’re not going to change any of our logic code. None. First let’s modify the control template. I’m putting these in my App.xaml resources so my app could use them globally. Here’s the full style for the new control template which has new content in there and a ToggleButton added (I’ll keep it collapsed here for the sake of making this post less verbose than it already is):
Now if you look you’ll see that the ToggleButton itself has a Style attribute, so here’s the Style we use for that:
Now the only thing we need to do is add the Style attribute for our AutoCompleteBox as reflected here (notice the added Style attribute):
And now when we look at our application running we have what looks to be an editable ComboBox:
The ComboBox behavior is enabled now via the ToggleButton and the MinimumPrefixLength attribute on the AutoCompleteBox (try changing it to 1 and you’ll see that you don’t get exactly the same behavior).
It’s little gems like this that should cause you to pay attention to the finer details of SDKs, samples, etc. I hope you found this helpful…if you have, please consider subscribing for more samples like this (and no I’m not planning on just emitting all the SDK samples :-)). You can download my code for this above in a complete solution here: ACBEditCombo.zip.