tags:

views:

663

answers:

2

Is it possible to avoid the automatic collapse of a Silverlight ComboBox after LostFocus?

+1  A: 

I don't think there is an easy way around this. The code below is copied from the disassembled code from the ComboBox Class. As you can see it closes always when hasFocus is false. I don't think there is any way around this. Writing your own ComboBox is a solution.

private void FocusChanged(bool hasFocus)
{
    this.UpdateSelectionBoxHighlighted();
    base.SetValueInternal(IsSelectionActiveProperty, hasFocus, true);
    if (!hasFocus)
    {
        this.IsDropDownOpen = false;
    }
}
Sorskoot
+1  A: 

Well, looking at the disassembled code it looks like

protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); this.FocusChanged(this.HasFocus()); }

is a good candidate for overwritting.

There's no way to solve your problem without implementing your own control subclass.

I've done the same to have a ComboBox with a Popup that doesn't close when I select an item (I want to have a multi-select behaviour).

If anyone is interested, here are my classes (works just fine for me as it is):

public class ComboBoxWithMultiSelect : ComboBox
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (base.IsDropDownOpen &&
                (e.Key == Key.Enter ||
                e.Key == Key.Space))
        {
            e.Handled = true;
        }
        else
        {
            base.OnKeyDown(e);
        }
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new ComboBoxItemWithMultiSelect();
    }
}


public class ComboBoxItemWithMultiSelect : ComboBoxItem
{
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        if (e == null)
        {
            throw new ArgumentNullException("e");
        }
        if (!e.Handled)
        {
            e.Handled = true;
        }
    }
}
R4cOON