7
7
using Microsoft . UI . Xaml ;
8
8
using Microsoft . UI . Xaml . Controls ;
9
9
using Microsoft . UI . Xaml . Markup ;
10
- using Microsoft . UI . Xaml . Media ;
11
10
using Microsoft . UI . Xaml . Media . Animation ;
11
+ using NavigationView = Microsoft . UI . Xaml . Controls . NavigationView ;
12
+ using NavigationViewSelectionChangedEventArgs = Microsoft . UI . Xaml . Controls . NavigationViewSelectionChangedEventArgs ;
12
13
13
14
namespace CommunityToolkit . WinUI . UI . Controls
14
15
{
@@ -37,11 +38,13 @@ public class TabbedCommandBar : NavigationView
37
38
public TabbedCommandBar ( )
38
39
{
39
40
DefaultStyleKey = typeof ( TabbedCommandBar ) ;
41
+ DefaultStyleResourceUri = new System . Uri ( "ms-appx:///Microsoft.Toolkit.Uwp.UI.Controls.Core/Themes/Generic.xaml" ) ;
40
42
41
43
// WinUI3 workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/3502
42
44
this . DefaultStyleResourceUri = new Uri ( "ms-appx:///CommunityToolkit.WinUI.UI.Controls.Core/Themes/Generic.xaml" ) ;
43
45
44
46
SelectionChanged += SelectedItemChanged ;
47
+ Loaded += TabbedCommandBar_Loaded ;
45
48
}
46
49
47
50
/// <inheritdoc/>
@@ -59,6 +62,15 @@ protected override void OnApplyTemplate()
59
62
_tabbedCommandBarContentBorder = GetTemplateChild ( "PART_TabbedCommandBarContentBorder" ) as Border ;
60
63
_tabChangedStoryboard = GetTemplateChild ( "TabChangedStoryboard" ) as Storyboard ;
61
64
65
+ // TODO: We could maybe optimize and use a lower-level Loaded event for what's hosting the MenuItems
66
+ // to set SelectedItem, but then we may have to pull in another template part, so think we're OK
67
+ // to do the Loaded event at the top level.
68
+ }
69
+
70
+ private void TabbedCommandBar_Loaded ( object sender , RoutedEventArgs e )
71
+ {
72
+ // We need to select the item after the template is realized, otherwise the SelectedItem's
73
+ // DataTemplate bindings don't properly navigate the visual tree.
62
74
SelectedItem = MenuItems . FirstOrDefault ( ) ;
63
75
}
64
76
0 commit comments