@@ -26,19 +26,10 @@ public class TabbedCommandBar : NavigationView
26
26
private Storyboard _tabChangedStoryboard = null ;
27
27
28
28
/// <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"/>.
30
30
/// </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 ;
42
33
43
34
/// <summary>
44
35
/// Initializes a new instance of the <see cref="TabbedCommandBar"/> class.
@@ -47,11 +38,7 @@ public TabbedCommandBar()
47
38
{
48
39
DefaultStyleKey = typeof ( TabbedCommandBar ) ;
49
40
50
- SelectionChanged += ( NavigationView sender , NavigationViewSelectionChangedEventArgs args ) =>
51
- {
52
- _ribbonContentBorder . Background = ( sender . SelectedItem as Control ) . Background ;
53
- _tabChangedStoryboard ? . Begin ( ) ;
54
- } ;
41
+ SelectionChanged += SelectedItemChanged ;
55
42
}
56
43
57
44
/// <inheritdoc/>
@@ -71,5 +58,44 @@ protected override void OnApplyTemplate()
71
58
72
59
SelectedItem = MenuItems . FirstOrDefault ( ) ;
73
60
}
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
+ }
74
100
}
75
101
}
0 commit comments