Skip to content

Commit 65362f0

Browse files
committed
Implemented logic to prevent hidden tabs from being selected
1 parent 6eec4b7 commit 65362f0

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/TabbedCommandBar/TabbedCommandBar.cs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,10 @@ public class TabbedCommandBar : NavigationView
2626
private Storyboard _tabChangedStoryboard = null;
2727

2828
/// <summary>
29-
/// Gets or sets the brush to use as the background for <see cref="TabbedCommandBarItem"/>s in MenuItems.
29+
/// The last selected <see cref="TabbedCommandBarItem"/>.
3030
/// </summary>
31-
public Brush ItemBackground
32-
{
33-
get => (Brush)GetValue(ItemBackgroundProperty);
34-
set => SetValue(ItemBackgroundProperty, value);
35-
}
36-
37-
/// <summary>
38-
/// Identifies the <see cref="ItemBackground"/> property.
39-
/// </summary>
40-
public static readonly DependencyProperty ItemBackgroundProperty =
41-
DependencyProperty.Register(nameof(ItemBackground), typeof(Brush), typeof(TabbedCommandBar), new PropertyMetadata(null));
31+
private TabbedCommandBarItem _previousSelectedItem = null;
32+
private long _visibilityChangedToken;
4233

4334
/// <summary>
4435
/// Initializes a new instance of the <see cref="TabbedCommandBar"/> class.
@@ -47,11 +38,7 @@ public TabbedCommandBar()
4738
{
4839
DefaultStyleKey = typeof(TabbedCommandBar);
4940

50-
SelectionChanged += (NavigationView sender, NavigationViewSelectionChangedEventArgs args) =>
51-
{
52-
_ribbonContentBorder.Background = (sender.SelectedItem as Control).Background;
53-
_tabChangedStoryboard?.Begin();
54-
};
41+
SelectionChanged += SelectedItemChanged;
5542
}
5643

5744
/// <inheritdoc/>
@@ -71,5 +58,44 @@ protected override void OnApplyTemplate()
7158

7259
SelectedItem = MenuItems.FirstOrDefault();
7360
}
61+
62+
private void SelectedItemChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
63+
{
64+
var item = sender.SelectedItem as TabbedCommandBarItem;
65+
if (item == null || item.Visibility == Visibility.Collapsed)
66+
{
67+
// If the item is now hidden, select the first item instead.
68+
// I can't think of any way that the visibiltiy would be null
69+
// and still be selectable, but let's handle it just in case.
70+
sender.SelectedItem = sender.MenuItems.FirstOrDefault();
71+
return;
72+
}
73+
74+
// Remove the visibility PropertyChanged handler from the
75+
// previously selected item
76+
if (_previousSelectedItem != null)
77+
{
78+
_previousSelectedItem.UnregisterPropertyChangedCallback(TabbedCommandBarItem.VisibilityProperty, _visibilityChangedToken);
79+
}
80+
81+
// Register a new visibility PropertyChangedcallback for the
82+
// currently selected item
83+
_previousSelectedItem = item;
84+
_visibilityChangedToken =
85+
_previousSelectedItem.RegisterPropertyChangedCallback(TabbedCommandBarItem.VisibilityProperty, SelectedItemVisibilityChanged);
86+
87+
// Set the ribbon background and start the transition animation
88+
_ribbonContentBorder.Background = item.Background;
89+
_tabChangedStoryboard?.Begin();
90+
}
91+
92+
private void SelectedItemVisibilityChanged(DependencyObject sender, DependencyProperty dp)
93+
{
94+
// If the item is not visible, default to the first tab
95+
if (sender.GetValue(dp) is Visibility vis && vis == Visibility.Collapsed)
96+
{
97+
SelectedItem = MenuItems.FirstOrDefault();
98+
}
99+
}
74100
}
75101
}

Microsoft.Toolkit.Uwp.UI.Controls/TabbedCommandBar/TabbedCommandBar.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<Setter Property="IsTabStop" Value="False" />
1616
<Setter Property="CompactPaneLength" Value="{ThemeResource NavigationViewCompactPaneLength}" />
1717
<Setter Property="Background" Value="{ThemeResource SystemChromeMediumLowColor}" />
18-
<Setter Property="ItemBackground" Value="{ThemeResource SystemChromeLowColor}" />
1918
<Setter Property="Template">
2019
<Setter.Value>
2120
<ControlTemplate TargetType="controls:TabbedCommandBar">

0 commit comments

Comments
 (0)