views:

23

answers:

1

I'm fairly new to Silverlight but experienced in web development, and I'm finding myself highly annoyed with Silverlight's default combobox. It seems to be lacking any concept of use for regular data entry. Primarily I'm wishing it would function like an HTML select box, where you can hit the drop down, then type a letter and it takes you down to the first item with that letter. Is there an easy way I'm missing to make it function like this, or a third party control that can do this?

Thanks!

+1  A: 

You could write an attached behavior to provide this functionality. The problem is that the items in a ComboBox in Silverlight aren't always strings. They may be entire controls that the user has templated as the ItemTemplate. If you know yours are going to be string you can implement a Behavior<ComboBox> to attach to the KeyDown event and select the correct one.

public class HTMLSelectBehavior : Behavior<ComboBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.KeyDown += OnKeyDown;
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        SelectedItem = AssociatedObject.ItemsSource
                           .FirstOrDefault(i => i.ToString().BeginsWith((char)e.Key));
    }
}

This is off the top of my head so it may not be exactly right and definitely lacks many safety checks, but it should give you an idea.

Stephan
That'll work, thanks!
Spencer