[英]How to propagate Home/End button press to a UWP RichEditBox inside of a WPF ScrollViewer?
In a WPF
application (targeting .NET core 3.1) on one of the windows, I have a ScrollViewer
and inside the ScrollViewer (among other elements) I placed a custom UWP
control, which contains a RichEditBox
. 我通过XamlHosts
添加了这个自定义 UWP 控件:
<Window xmlns:xaml="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost">
...
<ScrollViewer>
<Grid>
...
<xaml:WindowsXamlHost InitialTypeName="UWPControls.MyRichBox" x:Name="UwpRichEditBox"/>
</Grid>
</ScrollViewer>
<Window>
UWP 样式的 RichEditBox 出现在 WPF 应用程序中,我可以输入文本,使用箭头按钮移动插入符号,但某些关键事件不起作用。 例如,我不能使用 Home/End 按钮来 go 到 RichEditBox 中一行的开头或结尾。
问题在于,WPF 应用程序中的 ScrollViewer 会捕获这些按钮按下(Home/End/Ctrl+Right-Left),并且不会传播到 Xaml Island RichEditBox 控件。 我知道这一点,因为如果我删除 ScrollViewer,问题就会消失。
我尝试为 ScrollViewer 设置Focusable="False"
和IsTabStop="False"
,但没有帮助。 我可以在 WPF 中捕获这些键盘事件,但在 XamlIsland UWP 控件中,当我按下 Home 或 End 按钮(也不是KeyDownEvent
,也不是PreviewKeyDownEvent
)时,根本不会触发这些事件。 对于其他键,它们会被触发。
我可以以某种方式阻止 ScrollViewer 捕获键盘事件吗? 或者,当我在 WPF 中捕获它们时,我能否以某种方式在 UWP RichEditBox 上引发键盘事件?
最后我可以通过子类化ScrollViewer
并覆盖OnKeyDown
方法来解决这个问题。
namespace MyNameSpace.Custom {
public class MyScrollViewer : ScrollViewer {
protected override void OnKeyDown(KeyEventArgs e) {
// do nothing!
}
}
}
然后在 XAML 视图中使用这个控件
<Window x:Class="..."
xmlns:custom="clr-namespace:MyNameSpace.Custom">
<custom:MyScrollViewer>
....
</custom:MyScrollViewer>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.