gpt4 book ai didi

WPF ComboBox 下拉列表阻止单击其他控件

转载 作者:行者123 更新时间:2023-12-05 06:41:34 25 4
gpt4 key购买 nike

我有一个像这样的简单表格:

enter image description here

我打开组合框,当下拉菜单打开时,我单击按钮。单击按钮时,我会显示一条简单消息,但当时未显示该消息。当我再次点击它时它会显示。

文本框也有同样的问题。当下拉菜单打开时,文本框点击不起作用。

为什么组合框在打开时会阻止点击其他控件?

最佳答案

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

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

然后在找到该控件后的 FilterCallback 函数中,在该控件上引发鼠标按下事件。

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 下拉列表阻止单击其他控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com