WPF ComboBox下拉列表阻止单击其他控件 [英] WPF ComboBox dropdown is preventing clicking other controls

查看:493
本文介绍了WPF ComboBox下拉列表阻止单击其他控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的形式,如下所示:

I have a simple form like this:

我打开组合框,然后在打开下拉列表时,单击按钮。在按钮上单击我会显示一条简单消息,但该消息当时未显示。
当我再次单击它时会显示。

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?

推荐答案

您可以为ComboBox DropDownClosed创建一个事件,并使用hittest函数找到用户单击的另一个控件。

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));
}

然后在找到该控件后,在FilterCallback函数中,将鼠标向下事件触发

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;
}

这篇关于WPF ComboBox下拉列表阻止单击其他控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆