1
votes

Normally, when ComboBox has drop-down opened I need two mouse clicks to give focus to other control. First click will close drop-down, second will give focus to other control. I need a way to give focus to other control just by one mouse click. Any idea how to do it in WPF?

1
What have you already tried?Makyen♦
I have tried to Override OnMouseDown. I dont't have other idea.Dawid Jablonski

1 Answers

0
votes

you can handle the DropDownClosed event of the combobox control like so:

private void comboBox_DropDownClosed(object sender, EventArgs e)
    {
        Point m = Mouse.GetPosition(this);
        VisualTreeHelper.HitTest(this, new HitTestFilterCallback(FilterCallback),
            new HitTestResultCallback(ResultCallback), new PointHitTestParameters(m));
    }
    private HitTestFilterBehavior FilterCallback(DependencyObject o)
    {
        var c = o as Control;
        if ((c != null) && !(o is MainWindow))
        {
            if (c.Focusable)
            {
                c.Focus();
                return HitTestFilterBehavior.Stop;
            }
        }
        return HitTestFilterBehavior.Continue;
    }

    private HitTestResultBehavior ResultCallback(HitTestResult r)
    {
        return HitTestResultBehavior.Continue;
    }

this is based on a solution provided here