简体   繁体   English

如何将 Home/End 按钮按下传播到 WPF ScrollViewer 内的 UWP RichEditBox?

[英]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 . 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 . I added this custom UWP control via XamlHosts :我通过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>

The UWP styled RichEditBox shows up in the WPF app, I can type text, move the caret with the arrow buttons, but some of the key events are not working. UWP 样式的 RichEditBox 出现在 WPF 应用程序中,我可以输入文本,使用箭头按钮移动插入符号,但某些关键事件不起作用。 For example I can't use the Home/End buttons to go to the beginning or to the ending of a line in the RichEditBox.例如,我不能使用 Home/End 按钮来 go 到 RichEditBox 中一行的开头或结尾。

The issue is that, the ScrollViewer in the WPF app catches these button presses (Home/End/Ctrl+Right-Left) and it's not propagated towards the Xaml Island RichEditBox control.问题在于,WPF 应用程序中的 ScrollViewer 会捕获这些按钮按下(Home/End/Ctrl+Right-Left),并且不会传播到 Xaml Island RichEditBox 控件。 I know this, because if I remove the ScrollViewer, the issue disappears.我知道这一点,因为如果我删除 ScrollViewer,问题就会消失。

I tried to set Focusable="False" and IsTabStop="False" for the ScrollViewer, but didn't help.我尝试为 ScrollViewer 设置Focusable="False"IsTabStop="False" ,但没有帮助。 I can catch these keyboard events in WPF, but in the XamlIsland UWP control the events are not fired at all when I press the Home or End buttons (nor the KeyDownEvent , neither the PreviewKeyDownEvent ).我可以在 WPF 中捕获这些键盘事件,但在 XamlIsland UWP 控件中,当我按下 Home 或 End 按钮(也不是KeyDownEvent ,也不是PreviewKeyDownEvent )时,根本不会触发这些事件。 For other keys they are fired.对于其他键,它们会被触发。

Can I somehow prevent the ScrollViewer to catch keyboard events?我可以以某种方式阻止 ScrollViewer 捕获键盘事件吗? Or can I somehow raise a keyboard event on a UWP RichEditBox when I catch them in WPF?或者,当我在 WPF 中捕获它们时,我能否以某种方式在 UWP RichEditBox 上引发键盘事件?

Finally I could solve this issue, by sub-classing the ScrollViewer and overriding the OnKeyDown method.最后我可以通过子类化ScrollViewer并覆盖OnKeyDown方法来解决这个问题。

namespace MyNameSpace.Custom {
    public class MyScrollViewer : ScrollViewer {
        protected override void OnKeyDown(KeyEventArgs e) {
            // do nothing!
        }
    }
}

Then use this control in XAML view然后在 XAML 视图中使用这个控件

<Window x:Class="..."
        xmlns:custom="clr-namespace:MyNameSpace.Custom">
    <custom:MyScrollViewer>
        ....
    </custom:MyScrollViewer>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM