Universal Windows Platform Split View Control

Windows 10 brings a lot of new features in Universal Windows App Platform. One of the new control that is integrated in Visual Studio is Split View control. This control is nothing more than navigation affordance. It gives a whole new experience to navigate between pages, also back buttonservicebenefit. Moreover, Split View is not only for navigation but also you can customize it as your application need.

The split view control can be used to make a nav pane pattern. To build this pattern, add an expand/collapse button (the "hamburger" button) and a list view need to the split view control.

Examples

The split view control in its default form is a basic container. With a button and a list view added, the split view control is ready as a navigation menu. Here are examples of the split view as a navigation menu, in expanded and compact modes.

navigation menu
                                            Figure 1

Creating a Project with Split View Control

Firstly, create a new project and in MainPage.xaml inside the Grid make a new SplitView control.

  1. <SplitView x:Name="SplitView" OpenPaneLength="240" CompactPaneLength="48"   
  2. DisplayMode="CompactOverlay" IsPaneOpen="False" PaneBackground="DarkGray">  
  3.     <SplitView.Pane>  
  4.         <StackPanel x:Name="SplitViewPanePanel">  
  5.             <RadioButton x:Name="BackRadioButton" Click="BackRadioButton_Click" Style="{StaticResource NavRadioButtonStyle}" Tag="" Background="Gray" Content="Back" GroupName="Back"/>  
  6.             <RadioButton x:Name="HamburgerRadioButton" Click="HamburgerRadioButton_Click" Style="{StaticResource NavRadioButtonStyle}" Tag="" Content="Menu" GroupName="Hamburger"/>  
  7.   
  8.             <RadioButton x:Name="HomeRadioButton" Click="HomeRadioButton_Click" Style="{StaticResource NavRadioButtonStyle}" Tag="" Content="Home" GroupName="Navigation"/>  
  9.             <RadioButton x:Name="SettingsRadioButton" Click="SettingsRadioButton_Click" Style="{StaticResource NavRadioButtonStyle}" Tag="" Content="Settings" GroupName="Navigation"/>  
  10.         </StackPanel>  
  11.     </SplitView.Pane>  
  12.     <SplitView.Content>  
  13.         <Frame Name="MyFrame">  
  14.   
  15.         </Frame>  
  16.     </SplitView.Content>  
  17. </SplitView>  
                                                    Listing 1

Here you can see that, in SplitView control there are two section, one is “SplitView.Pane” and another is “SplitView.Content”. “SplitView.Pane” holds all the list items of your navigation links, which you can see in Figure 1 and “SplitView.Content” is the main frame of your application, which you can populate with other controls and application data whatsoever. We have taken a Frame inside the “SplitView.Content” because we want to make the Split View fixed and load different pages as needed. So, the user will notice that a new frame will be loaded instead of a new page. That will give a great experience to your application.

Simple custom RadioButtonStyle

To set the icon of the “SplitView.Pane”, we have used a custom style of Radio Button. And in the Radio Button their is a Tag property which you can use the Character Map Icons.

Character Map Icons
                                                Figure 2

We have put the style code outside the grid. The custom style code snippet is given below.
  1. <Page.Resources>  
  2.     <ResourceDictionary>  
  3.         <SolidColorBrush x:Key="NavButtonPressedBackgroundBrush" Color="White" Opacity="0.3" />  
  4.         <SolidColorBrush x:Key="NavButtonCheckedBackgroundBrush" Color="White" Opacity="0.2" />  
  5.         <SolidColorBrush x:Key="NavButtonHoverBackgroundBrush" Color="White" Opacity="0.1" />  
  6.   
  7.         <Style x:Key="NavRadioButtonStyle" TargetType="RadioButton">  
  8.             <Setter Property="Background" Value="Transparent"/>  
  9.             <Setter Property="Padding" Value="3"/>  
  10.             <Setter Property="HorizontalAlignment" Value="Stretch"/>  
  11.             <Setter Property="VerticalAlignment" Value="Center"/>  
  12.             <Setter Property="HorizontalContentAlignment" Value="Left"/>  
  13.             <Setter Property="VerticalContentAlignment" Value="Center"/>  
  14.             <Setter Property="Template">  
  15.                 <Setter.Value>  
  16.                     <ControlTemplate TargetType="RadioButton">  
  17.                         <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">  
  18.                             <VisualStateManager.VisualStateGroups>  
  19.                                 <VisualStateGroup x:Name="CommonStates">  
  20.                                     <VisualState x:Name="Normal"/>  
  21.                                     <VisualState x:Name="PointerOver">  
  22.                                         <Storyboard>  
  23.                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundGrid">  
  24.                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource NavButtonHoverBackgroundBrush}"/>  
  25.                                             </ObjectAnimationUsingKeyFrames>  
  26.                                         </Storyboard>  
  27.                                     </VisualState>  
  28.                                     <VisualState x:Name="Pressed">  
  29.                                         <Storyboard>  
  30.                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundGrid">  
  31.                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource NavButtonPressedBackgroundBrush}"/>  
  32.                                             </ObjectAnimationUsingKeyFrames>  
  33.                                         </Storyboard>  
  34.                                     </VisualState>  
  35.                                     <VisualState x:Name="Disabled" />  
  36.                                 </VisualStateGroup>  
  37.                                 <VisualStateGroup x:Name="CheckStates">  
  38.                                     <VisualState x:Name="Checked">  
  39.                                         <Storyboard>  
  40.                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundGrid">  
  41.                                                 <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource NavButtonCheckedBackgroundBrush}"/>  
  42.                                             </ObjectAnimationUsingKeyFrames>  
  43.                                         </Storyboard>  
  44.                                     </VisualState>  
  45.                                     <VisualState x:Name="Unchecked"/>  
  46.                                     <VisualState x:Name="Indeterminate"/>  
  47.                                 </VisualStateGroup>  
  48.                                 <VisualStateGroup x:Name="FocusStates">  
  49.                                     <VisualState x:Name="Focused"/>  
  50.                                     <VisualState x:Name="Unfocused"/>  
  51.                                     <VisualState x:Name="PointerFocused"/>  
  52.                                 </VisualStateGroup>  
  53.                             </VisualStateManager.VisualStateGroups>  
  54.                             <Grid Name="BackgroundGrid" Background="Transparent" VerticalAlignment="Stretch">  
  55.                                 <Grid.ColumnDefinitions>  
  56.                                     <ColumnDefinition Width="48"/>  
  57.                                     <ColumnDefinition Width="*"/>  
  58.                                 </Grid.ColumnDefinitions>  
  59.                                 <TextBlock FontSize="34" Height="38" Text="{TemplateBinding Tag}" FontFamily="Segoe MDL2 Assets" Margin="5,8,5,5" VerticalAlignment="Center" HorizontalAlignment="Center"/>  
  60.                                 <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" TextWrapping="Wrap" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>  
  61.                             </Grid>  
  62.                         </Border>  
  63.                     </ControlTemplate>  
  64.                 </Setter.Value>  
  65.             </Setter>  
  66.         </Style>  
  67.     </ResourceDictionary>  
  68. </Page.Resources>  
                                                                 Listing 2

You can also create your custom styles using Blend. To do this, open a blank project in Blend or open your existing project like the following screenshot:

Design in blend
                                                         Figure 3

In main Grid, take a Radio Button control.

radio button
                                                   Figure 4

Right click on the control and select Edit Template and under Edit Template option select Edit a Copy.

Edit a copy
                                               Figure 5

Then you can customize the style according to your need.

After everything has been set, the MainPage.xaml will look like this.

Main page
                     Figure 6


Adding Extra Pages

Now, create two blank pages named Page1.xaml and Page2.xaml. In Page1.xaml, change the code like below.
  1. <Grid Background="LightBlue">  
  2.    <TextBlock FontSize="36" Text="Home Page" Margin="12,17,0,17"/>  
  3. </Grid>  
                                                                 Listing 3

Similarly, in Page2.xaml change the code like below:
  1. <Grid Background="SteelBlue">  
  2.    <TextBlock FontSize="36" Text="Settings Page" Margin="12,17,0,17"/>  
  3. </Grid>  
                                                                  Listing 4

Handle events in code-behind MainPage.xaml.cs

The event handlers of the Split View’s Radio Buttons are as follows.
  1. public MainPage()  
  2. {  
  3.     this.InitializeComponent();  
  4.     MyFrame.Navigate(typeof(Page1));  
  5. }  
  6.   
  7. private void BackRadioButton_Click(object sender, RoutedEventArgs e)  
  8. {  
  9.     if (MyFrame.CanGoBack)  
  10.     {  
  11.         MyFrame.GoBack();  
  12.     }  
  13. }  
  14.   
  15. private void HamburgerRadioButton_Click(object sender, RoutedEventArgs e)  
  16. {  
  17.     if (!this.SplitView.IsPaneOpen)  
  18.     {  
  19.         this.SplitView.IsPaneOpen = true;  
  20.     }  
  21.     else  
  22.     {  
  23.         this.SplitView.IsPaneOpen = false;  
  24.     }  
  25. }  
  26.   
  27. private void HomeRadioButton_Click(object sender, RoutedEventArgs e)  
  28. {  
  29.     MyFrame.Navigate(typeof(Page1));  
  30. }  
  31.   
  32. private void SettingsRadioButton_Click(object sender, RoutedEventArgs e)  
  33. {  
  34.       
                                                      Listing: 5

Here, we’ve load the Page1 inside the Frame name “MyFrame” in MainPage.xaml. In MainPage.xaml.cs we have navigated to Page1 in Constructor. So, when the application starts, it will open the Page1 under MyFrame control. Back Button event handler checks whether the frame can go back. If so it goes back to the previous page otherwise not. And Hamburger Button checks if the SplitView is open or not. If not open, it will open it and if it is open, then it will close it. Home and Settings Button do the basis navigation service.

Running the Application

If you run the application in local machine, look like this.

Local Machine:

Running the Application
                                                                  Figure 7

And if you click the Settings button, it will navigate to the Settings page.

Settings button
                           Figure 8

Running is the Windows Phone:

Setting page
                                                                  Figure 9

Improve it a bit for Windows Phone

Sometimes your design may not fit for both desktop and mobile devices. We have implement a Back Button in our Split View control, as desktop application do not have a Back Button integrated. But mobile devices have their own hardware or OS (Operating System) integrated Back Button. So, by determining device type we can customize our application UI (User Interface). You can do this, by simply install a NuGet package named “WindowsStateTriggers”.

WindowsStateTriggers
                                                                     Figure 10

Install it and now put the reference in your MainPage.xam, inside the Page tag.
  1. <Page  
  2.     x:Class="SplitView.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:SplitView"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d"  
  9.       
  10.     xmlns:triggers="using:WindowsStateTriggers">  
                                                      Listing 6

We have named the reference “triggers”. Now, we can use it in our Xaml code. To determine the device type, we have to use State Trigger like below.

 

  1. <VisualStateManager.VisualStateGroups>  
  2.     <VisualStateGroup>  
  3.         <VisualState x:Name="phone">  
  4.             <VisualState.StateTriggers>  
  5.                 <triggers:DeviceFamilyStateTrigger DeviceFamily="Mobile" />  
  6.             </VisualState.StateTriggers>  
  7.             <VisualState.Setters>  
  8.                 <Setter Target="BackRadioButton.Visibility" Value="Collapsed" />  
  9.             </VisualState.Setters>  
  10.         </VisualState>  
  11.     </VisualStateGroup>  
  12. </VisualStateManager.VisualStateGroups>  
                                                              Listing 7

In this State Triggers we have set Device Family to “Mobile” and set the value of Back Button to Collapsed. We have to put this inside the main Grid. This will hide the Back Button when the application will run in mobile devices. If we run the application, it will look like this.

Homepage
                     Figure 10

You can learn further about Windows State Triggers at Windows State Triggers.

Important things to notice

In SplitView control several things, you need to consider. First of all, is Display Mode. There are four types of Display Mode, Compact Inline, Compact Overlay, Inline and Overlay. If you use Compact Inline the Split View Content will move right unlike using Compact Overlay.

Menu
                                             Figure 11

OpenPaneLength determines the width of the SplitView Pane’s width and CompactPaneLength determines the iconic view’s width. You can also set Pane Background Color. We have set here Dark Gray. Moreover, the Tag property of Radio Button is to set the icon of the Radio Button. You can choose it from Character Map, shown in Figure 2. Radio Buttons have a Tag set to a certain character. The new character set is Segoe MDL2 Assets, and the characters used are from that set.

Conclusion

Split View is a very useful control that introduced in Windows 10 Universal App. It will give you the power to do whatever you want. You can use it instead of Bottom App Bar, but still you can use it alongside. Hope that you get the minimum understanding of using Split View control and can get started using in your application. Happy coding!

Download the full source code at, http://1drv.ms/1N1k1kn


Similar Articles