[英]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.