gpt4 book ai didi

WPF ContextMenu 单击路由到 WinForms 应用程序

转载 作者:行者123 更新时间:2023-12-02 04:17:26 24 4
gpt4 key购买 nike

我正在编写一个 WPF 控件,该控件托管在 Word VSTO AddIn (WinForms) 中。现在我遇到了上下文菜单上的鼠标单击事件的问题。

如果我单击左半部分(WinForms 应用程序上方的部分)的上下文菜单项,则单击将直接转到 WinForms 应用程序,并且我的上下文菜单不会收到该事件。

如果我单击该项目的右半部分(WPF 表单上的部分),一切都会按预期工作。

Illustrated the issue

有人可以帮我解决这个问题吗?

最佳答案

非活跃博客的答案是:

声明类级别调度程序框架对象

System.Windows.Threading.DispatcherFrame _frame;

订阅菜单的 GotFocusEvent 和 LostFocusEvent:

_menu.AddHandler(System.Windows.UIElement.GotFocusEvent,new RoutedEventHandler(OnGotFocusEvent));
_menu.AddHandler(System.Windows.UIElement.LostFocusEvent, new RoutedEventHandler(OnLostFocusEvent));

下面是 GotFocusEvent 和 LostFocusEvent 事件过程的实现:

private void OnGotFocusEvent(object sender, RoutedEventArgs e)
{
if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal (DispatcherOperationCallback)delegate(object unused)
{
_frame = new DispatcherFrame();
Dispatcher.PushFrame(_frame);
return null;
}, null);
}
}

private void OnLostFocusEvent(object sender, RoutedEventArgs e)
{
if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu)
{
_frame.Continue = false;
}
}

就我而言,不需要 if 语句,我订阅了这样的事件

<EventSetter Event="GotFocus" Handler="contextMenu_GotFocus" />
<EventSetter Event="LostFocus" Handler="contextMenu_LostFocus" />

关于WPF ContextMenu 单击路由到 WinForms 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11609874/

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