Skip to content

Commit

Permalink
Fix NavView keyboard navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Apr 27, 2020
1 parent b94eb0c commit 5845073
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
18 changes: 18 additions & 0 deletions ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ public override void OnApplyTemplate()

UnhookEventsAndClearFields();

IControlProtected controlProtected = this;

// Set up the pane toggle button click handler
if (GetTemplateChild(c_togglePaneButtonName) is Button paneToggleButton)
{
Expand Down Expand Up @@ -578,6 +580,22 @@ public override void OnApplyTemplate()
closeButtonToolTip.Content = navigationCloseButtonToolTip;
}

if (SharedHelpers.IsRS2OrHigher())
{
// Get hold of the outermost grid and enable XYKeyboardNavigationMode
// However, we only want this to work in the content pane + the hamburger button (which is not inside the splitview)
// so disable it on the grid in the content area of the SplitView
if (GetTemplateChildT<Grid>(c_rootGridName, controlProtected) is { } rootGrid)
{
KeyboardNavigation.SetDirectionalNavigation(rootGrid, KeyboardNavigationMode.Contained);
}

if (GetTemplateChildT<Grid>(c_contentGridName, controlProtected) is { } contentGrid)
{
KeyboardNavigation.SetDirectionalNavigation(contentGrid, KeyboardNavigationMode.None);
}
}

// TODO: WPF - AccessKey
//m_accessKeyInvokedRevoker = AccessKeyInvoked(winrt::auto_revoke, { this, &NavigationView::OnAccessKeyInvoked });

Expand Down
7 changes: 5 additions & 2 deletions ModernWpf.Controls/NavigationView/NavigationView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,8 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NavigationView">
<Grid x:Name="RootGrid">
<Grid x:Name="RootGrid"
KeyboardNavigation.TabNavigation="Local">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="DisplayModeGroup">
<VisualState x:Name="Compact" />
Expand Down Expand Up @@ -961,7 +962,9 @@
Margin="0,0,0,8"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Canvas.ZIndex="100">
Canvas.ZIndex="100"
KeyboardNavigation.TabNavigation="Local"
KeyboardNavigation.TabIndex="0">

<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
Expand Down
5 changes: 5 additions & 0 deletions ModernWpf.Controls/NavigationView/NavigationViewItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ModernWpf.Controls
{
Expand All @@ -18,6 +19,10 @@ static NavigationViewItemBase()
VerticalContentAlignmentProperty.OverrideMetadata(
typeof(NavigationViewItemBase),
new FrameworkPropertyMetadata(VerticalAlignment.Center));

KeyboardNavigation.DirectionalNavigationProperty.OverrideMetadata(
typeof(NavigationViewItemBase),
new FrameworkPropertyMetadata(KeyboardNavigationMode.None));
}

internal NavigationViewItemBase()
Expand Down
7 changes: 7 additions & 0 deletions ModernWpf.Controls/Repeater/ItemsRepeater/ItemsRepeater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using ModernWpf.Automation.Peers;
Expand All @@ -20,6 +21,12 @@ public partial class ItemsRepeater : Panel
// A convention we use in the ItemsRepeater codebase for an invalid Rect value.
internal static readonly Rect InvalidRect = Rect.Empty;

static ItemsRepeater()
{
KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(ItemsRepeater),
new FrameworkPropertyMetadata(KeyboardNavigationMode.Once));
}

public ItemsRepeater()
{
AnimationManager = new AnimationManager(this);
Expand Down
9 changes: 6 additions & 3 deletions ModernWpf.Controls/SplitView/SplitView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SplitView">
<Grid Background="{TemplateBinding Background}">
<Grid Background="{TemplateBinding Background}"
KeyboardNavigation.TabNavigation="Local">

<Grid.Resources>
<Storyboard x:Key="FromClosedToOpenOverlayLeft">
Expand Down Expand Up @@ -592,15 +593,17 @@
<ColumnDefinition x:Name="ColumnDefinition1" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.OpenPaneGridLength, FallbackValue={StaticResource ZeroGridLength}}" />
<ColumnDefinition x:Name="ColumnDefinition2" Width="*" />
</Grid.ColumnDefinitions>
<!-- Pane Content Area -->
<!-- Pane Content Area -->
<Grid
x:Name="PaneRoot"
Grid.ColumnSpan="2"
HorizontalAlignment="Left"
Visibility="Collapsed"
Background="{TemplateBinding PaneBackground}"
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.OpenPaneLength}"
Canvas.ZIndex="1">
Canvas.ZIndex="1"
KeyboardNavigation.TabNavigation="Local"
KeyboardNavigation.TabIndex="0">
<Grid.Clip>
<RectangleGeometry x:Name="PaneClipRectangle">
<RectangleGeometry.Transform>
Expand Down

0 comments on commit 5845073

Please sign in to comment.