Top Navigation Bar Using XAML

If you have checked many Windows Store apps that use the top app bar, there is no sample available for it. If you have checked, it is given for HTML only. So today, I am going to show you how we can create the same type of app bar using XAML. I have also attached the code with this blog. Let's start.
 
Step 1

Create Windows 8.1 blank template.
 
 
Step 2

Add user control to it.

Right click on project -> Add -> new Item -> User control.

Now, we are done with adding user control. Let's start with creating an app bar.
 
For creating an app bar, let's add GridView inside Grid in your usercontrol. In the Grid, we can add multiple items so I am taking a Grid to create app bar. You can also take a listview.
 
I have added a lot of things in GridView, that I will explain later. So, the code will look like below. 
  1. <Grid Background="{ThemeResource applicationPageBackgroundThemeBrush}">  
  2.        <GridView x:Name="grdView" ItemsSource="{Binding NavigationItemList}" Margin="0,-4,0,8" Height="124" SelectionMode="None" IsItemClickEnabled="true" IsSwipeEnabled="false" Background="#0B4A58"   
  3.                    Width="{Binding WindowWidth}" ItemClick="grdView_ItemClick"  
  4.                    ItemTemplate="{StaticResource NavigationDataTemplate}" ItemContainerStyle="{StaticResource GridViewItemStyle}">  
  5.            <GridView.ItemsPanel>  
  6.                <ItemsPanelTemplate>  
  7.                    <WrapGrid Orientation="Horizontal"/>  
  8.                </ItemsPanelTemplate>  
  9.            </GridView.ItemsPanel>  
  10.        </GridView>  
  11.    </Grid>  
If you are an XAML developer, you will understand this code but for the new users, I am explaining this code.
 
To display data in GridView, we need some collection and we have to set that with Itemssource property of GridView.
ItemTemplate is where we define the controls that we have to show in GridView and bind the controls.
I have used WrapGrid inside ItemsPanelTemplate as we need to show items horizontally. 
 
Now, I will show what template and style I have added for it. I have created this style using expression blend. Add this style and template in your usercontrol.
  1. <UserControl.Resources>  
  2.        <DataTemplate x:Key="NavigationDataTemplate">  
  3.            <StackPanel Orientation="Horizontal" Background="{Binding Color}" Width="140" Height="124" Margin="-11,0,0,0">  
  4.                <Border BorderBrush="Transparent" VerticalAlignment="Center" Width="138" Height="124" >  
  5.                    <StackPanel VerticalAlignment="Center">  
  6.                        <Image Source="{Binding image}"  Height="60" Width="60"></Image>  
  7.                        <TextBlock Text="{Binding Title}" FontSize="15" Foreground="White" HorizontalAlignment="Center" Padding="0 10 0 0"/>  
  8.                    </StackPanel>  
  9.                </Border>  
  10.                <Line Stroke="#1A8FAE" StrokeThickness="2" X1="0" X2="0" Y1="0" Y2="130"></Line>  
  11.            </StackPanel>  
  12.        </DataTemplate>  
  13.   
  14.   
  15.        <Style x:Key="GridViewItemStyle" TargetType="GridViewItem">  
  16.            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>  
  17.            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>  
  18.            <Setter Property="Background" Value="Transparent"/>  
  19.            <Setter Property="TabNavigation" Value="Local"/>  
  20.            <Setter Property="IsHoldingEnabled" Value="True"/>  
  21.            <Setter Property="Margin" Value="0,0,2,2"/>  
  22.            <Setter Property="Template">  
  23.                <Setter.Value>  
  24.                    <ControlTemplate TargetType="GridViewItem">  
  25.                        <GridViewItemPresenter x:Name="gridViewItemPresenter" ContentMargin="4" ContentTransitions="{TemplateBinding ContentTransitions}"  
  26.                                               CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"   
  27.                                               DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"   
  28.                                               DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"   
  29.                                               DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"   
  30.                                               DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"   
  31.                                               HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"   
  32.                                               Padding="{TemplateBinding Padding}"   
  33.                                               PointerOverBackgroundMargin="1"   
  34.                                               ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"   
  35.                                               SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}"   
  36.                                               SelectedBorderThickness="{ThemeResource GridViewItemCompactSelectedBorderThemeThickness}"   
  37.                                               VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">  
  38.                            <VisualStateManager.VisualStateGroups>  
  39.                                <VisualStateGroup x:Name="SelectionStates">  
  40.                                    <VisualState x:Name="UnselectedSwiping"/>  
  41.                                    <VisualState x:Name="UnselectedPointerOver"/>  
  42.                                    <VisualState x:Name="Selecting"/>  
  43.                                    <VisualState x:Name="Selected"/>  
  44.                                    <VisualState x:Name="SelectedSwiping"/>  
  45.                                    <VisualState x:Name="Unselecting"/>  
  46.                                    <VisualState x:Name="Unselected"/>  
  47.                                    <VisualState x:Name="SelectedUnfocused"/>  
  48.                                </VisualStateGroup>  
  49.                            </VisualStateManager.VisualStateGroups>  
  50.                        </GridViewItemPresenter>  
  51.                    </ControlTemplate>  
  52.                </Setter.Value>  
  53.            </Setter>  
  54.        </Style>  
  55.    </UserControl.Resources>  
Now, we are done with the UI part. Let's do C# part. Add the following code in usercontrol.cs file. I have added only two items for demo purposes. You can add multiples by adding it in a NavigationItemList.
  1. public ObservableCollection<NavigationItem> NavigationItemList  
  2.         { getset; }  
  3.   
  4.         private NavigationItem selectedItem;  
  5.         public NavigationItem SelectedItem  
  6.         {  
  7.             get  
  8.             {  
  9.                 return selectedItem;  
  10.             }  
  11.             set  
  12.             {  
  13.                 if (selectedItem != null)  
  14.                 {  
  15.                     selectedItem.IsSelected = false;  
  16.                 }  
  17.                 selectedItem = value;  
  18.   
  19.                 if (selectedItem != null)  
  20.                 {  
  21.                     selectedItem.IsSelected = true;  
  22.                 }  
  23.             }  
  24.         }  
  25.   
  26.         NavigationItem.NavigationItemType itemType = NavigationItem.NavigationItemType.NONE;  
  27.   
  28.         public NavigationBar()  
  29.         {  
  30.             this.InitializeComponent();  
  31.             NavigationItemList = new ObservableCollection<NavigationItem>();  
  32.   
  33.             NavigationItemList.Add(new NavigationItem { Type = NavigationItem.NavigationItemType.HOME, Title = "Home", image = "ms-appx:/Assets/AppBarIcons/Home.png" });  
  34.             NavigationItemList.Add(new NavigationItem { Type = NavigationItem.NavigationItemType.ASSIGNMENT, Title = "Assignment", image = "ms-appx:/Assets/AppBarIcons/Assignments.png" });  
  35.   
  36.             grdView.ItemsSource = NavigationItemList;  
  37.             SelectedItem = itemType != NavigationItem.NavigationItemType.NONE ? NavigationItemList.ElementAt<NavigationItem>((int)itemType) : null;  
  38.         }  
  39.   
  40.         private void grdView_ItemClick(object sender, ItemClickEventArgs e)  
  41.         {  
  42.             SelectedItem = (NavigationItem)e.ClickedItem;  
  43.   
  44.         }  
  45.   
  46.         public class NavigationItem : INotifyPropertyChanged  
  47.         {  
  48.             public string Title { getset; }  
  49.   
  50.             public string image { getset; }  
  51.   
  52.             private bool isSelected;  
  53.             public bool IsSelected  
  54.             {  
  55.                 get { return isSelected; }  
  56.                 set  
  57.                 {  
  58.                     isSelected = value;  
  59.                     color = isSelected ? "#1A8FAE" : "#0B4A58";  
  60.                     NotifyPropertyChanged("IsSelected");  
  61.                     NotifyPropertyChanged("Color");  
  62.                 }  
  63.             }  
  64.   
  65.             public NavigationItemType Type { getset; }  
  66.   
  67.             private string color;  
  68.   
  69.   
  70.             public string Color  
  71.             {  
  72.                 get { return color; }  
  73.                 set  
  74.                 {  
  75.                     color = value;  
  76.                     NotifyPropertyChanged("Color");  
  77.                 }  
  78.             }  
  79.   
  80.             public enum NavigationItemType  
  81.             {  
  82.                 NONE = -1,  
  83.                 HOME = 0,  
  84.                 ASSIGNMENT,  
  85.                 TESTS,  
  86.                 // LEARNINGPATH,  
  87.                 MYLIBRARY,  
  88.                 SCORECARD,  
  89.                 DISCUSSIONBOARD  
  90.             }  
  91.   
  92.             public event PropertyChangedEventHandler PropertyChanged;  
  93.   
  94.             // This method is called by the Set accessor of each property.  
  95.             // The CallerMemberName attribute that is applied to the optional propertyName  
  96.             // parameter causes the property name of the caller to be substituted as an argument.  
  97.             private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
  98.             {  
  99.                 if (PropertyChanged != null)  
  100.                 {  
  101.                     PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  102.                 }  
  103.             }  
  104.         }   
Step 3

Now, add one blank page in the project and add this user control in it as below.
  1. <Page.TopAppBar>  
  2.         <AppBar Background="#1A8FAE" BorderBrush="Transparent">  
  3.             <local:NavigationBar></local:NavigationBar>  
  4.         </AppBar>  
  5.     </Page.TopAppBar>  
 And it's done :)
 
The output of this is as below. Launch the app, right click on the screen, and it will display this app bar. Once you click on any item; i.e. Home, it will change its color and will disappear.
Ebook Download
View all
Learn
View all