gpt4 book ai didi

c# - Winforms 中的 WPF 事件

转载 作者:太空狗 更新时间:2023-10-29 20:25:41 25 4
gpt4 key购买 nike

我有一个 Winforms 应用程序,它在 ElementHost 中使用 WPF 控件(Avalon Edit,如果重要的话)。

它似乎工作正常,但我希望能够以 Winforms 方式处理此控件的 KeyPress 事件(没有 RoutedCommands 和 InputGestures),所以我虽然可以使用 KeyPreview 集处理 Form 的 KeyDown 事件,但 WPF 事件似乎并没有冒泡到表单。

那么基本上,您如何以 Winforms 方式访问 WPF 控件上的 KeyDown 事件?

最佳答案

您可以尝试为 WpfControl 本身添加自定义事件处理程序,而不是尝试连接到 WinForm 的 KeyDown。

举个例子。假设:您的 WinForm 是 Form1 类型,WpfControl 是 UserControl1,WpfControl 的元素宿主称为(永远不会猜到))- elementHost。

public Form1()
{
InitializeComponent();
elementHost.ChildChanged += ElementHost_ChildChanged;
}

private void ElementHost_ChildChanged(object sender, ChildChangedEventArgs e)
{
var ctr = (elementHost.Child as UserControl1);
if (ctr == null)
return;
ctr.KeyDown += ctr_KeyDown;
}

void ctr_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
/* your custom handling for key-presses */
}

UPD:e.KeyboardDevice.Modifiers(e 是 System.Windows.Input.KeyEventArgs)存储有关 Ctrl、Alt 等的信息。

关于c# - Winforms 中的 WPF 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4350862/

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