eoldre
eoldre

Reputation: 1087

WPF Combobox selection behavior

WPF ComboBox controls allow two ways to change the selection with the mouse.

Is there a way to eliminate behavior #2? Ie make them perform 2 full down/up clicks to change the selection?

Upvotes: 1

Views: 1226

Answers (1)

Sphinxxx
Sphinxxx

Reputation: 13057

It may not not pretty, but combining a few events seems to do what you're after:

private bool _comboMouseDown = false;
private bool _comboSelectionDisabled = false;

private void ComboBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    _comboMouseDown = true;
}

private void ComboBox_DropDownOpened(object sender, EventArgs e)
{
    if (_comboMouseDown)
    {
        //Don't enable selection until the user releases the mouse button:
        _comboSelectionDisabled = true;
    }
}

private void ComboBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    if (_comboSelectionDisabled)
    {
        //Stop the accompanying "MouseUp" event (which would select an item) from firing:
        e.Handled = true;

        _comboSelectionDisabled = false;
    }

    _comboMouseDown = false;
}

1) Still works as normal

2) Click-and-hold still opens the popup, but you need to release and click again to select an item.

Upvotes: 2

Related Questions