3

I have a simple form like this:

enter image description here

I open the combobox and at the time dropdown is open, I click the button. On button click I show a simple message but the message is not shown at that time. It shows when I click it again.

The same problem for textbox. When the dropdown is open, the textbox click is not working.

Why does combobox prevent clicking other controls when it is open?

MSL
  • 990
  • 1
  • 12
  • 28

2 Answers2

1

You can create an event for ComboBox DropDownClosed and with the hittestfunction, find the other control that the user has clicked.

private void ComboBox_DropDownClosed(object sender, EventArgs e)
{
    Point m = Mouse.GetPosition(this);
    VisualTreeHelper.HitTest(this, this.FilterCallback, this.ResultCallback, new PointHitTestParameters(m));
}

Then in the FilterCallback function after finding that control, raise the mouse down event on that control.

private HitTestFilterBehavior FilterCallback(DependencyObject o)
{
    var c = o as Control;
    if ((c != null) && !(o is MainWindow))
    {
        if (c.Focusable)
        {
            if (c is ComboBox)
            {
                (c as ComboBox).IsDropDownOpen = true;
            }
            else
            {
                var mouseDevice = Mouse.PrimaryDevice;
                var mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, 0, MouseButton.Left)
                {
                    RoutedEvent = Mouse.MouseDownEvent,
                    Source = c
                };
                c.RaiseEvent(mouseButtonEventArgs);
            }

            return HitTestFilterBehavior.Stop;
        }
    }
    return HitTestFilterBehavior.Continue;
}

private HitTestResultBehavior ResultCallback(HitTestResult r)
{
    return HitTestResultBehavior.Continue;
}
abcdefgh
  • 26
  • 3
0

The combobox is implemented the way that it captures the mouse when the dropdown is open. This is done to easyly figure out when the user clicks outside of the combobox (in fact it's a one-liner). When the user clicks outside of the combobox it releases the mouse, closes the dropdown and marks the click as handled. The last action of course stops further processing and the click is not passed to the control you thought you clicked on.

My personal opinion is this behavior has pros and cons. Microsoft decided the way it is.

gomi42
  • 2,449
  • 1
  • 14
  • 9
  • I want the button be clicked when dropdown is open. how to override the default way? – MSL Oct 16 '16 at 16:30