I have an issue with WebView2 for WPF. Inside a ScrollViewer, WebView2 lose focus at arrow keys press. Removing the ScrollViewer, the WebView2 focus works correctly, why? There is a way to fix this?
In WebView2 is rendered an html editor but this is not the cause of the problem.
Example of xaml
<ScrollViewer>
<StackPanel>
<TextBox />
<TextBox />
<TextBox />
<TextBox />
<wpf:WebView2 x:Name="wb" Height="200" />
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
</ScrollViewer>
Specs:
- .net 4.5
- Microsoft Edge WebView2 Runtime 100.0.1185.44
I have created a test project here
CodePudding user response:
Looking into it, it looks like that's because it's how ScrollViewer works - arrow keys are scroll events.
A workaround would be to create a handler for the PreviewKeyDown event. If you were to tweak the markup for your ScrollViewer to something like the following:
<ScrollViewer x:Name="scroller" PreviewKeyDown="scroller_PreviewKeyDown">
<StackPanel>
<TextBox />
<TextBox />
<TextBox />
<TextBox />
<wpf:WebView2 x:Name="wb" Height="200" />
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
</ScrollViewer>
You could then do this in the code behind:
private void scroller_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Up || e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Left || e.Key == Windows.System.VirtualKey.Right)
{
e.Handled = true;
}
}
It just means that you won't be able to scroll using the arrows.
