Modern UI For WPF Application by Example (Default Window Without Back Button)

Scope

The purpose of this article is to show how to create a default window in WPF using the Modern UI, but without the back button.

Introduction

Modern UI is a set of controls and styles converting our WPF application into a great looking Modern UI app. The Modern UI project can be found in mui.codeplex.com. Here it is possible to get the WPF app that demostrates the features provided by “mui”.

Description

In the sample Modern UI for WPF application by example (Default Window) we saw the default window provided by Modern UI that contains a back button. Now we will change the ModernWindow style to remove the back button.

To begin, we need to create, in our WPF application, a themes folder where we will add the resource dictionary, got from the original project, that contains the original style for ModernWindow.

Style for modern window

After it, we need to search in the style for the back button, as in the following:

modern window xmal

We can delete the button or only comment out the code and we need to define the key for the style. Here is the complete style.

  1. <Style TargetType="mui:ModernWindow" x:Key="MyModernWindow">  
  2.        <Setter Property="BackgroundContent" Value="{DynamicResource WindowBackgroundContent}" />  
  3.        <Setter Property="FontFamily" Value="{DynamicResource DefaultFontFamily}" />  
  4.        <Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />  
  5.        <Setter Property="Foreground" Value="{DynamicResource WindowText}" />  
  6.        <Setter Property="BorderBrush" Value="{DynamicResource WindowBorder}" />  
  7.        <Setter Property="Width" Value="800" />  
  8.        <Setter Property="Height" Value="640" />  
  9.        <Setter Property="MinWidth" Value="320" />  
  10.        <Setter Property="MinHeight" Value="320" />  
  11.        <Setter Property="ResizeMode" Value="CanResizeWithGrip" />  
  12.        <Setter Property="UseLayoutRounding" Value="True" />  
  13.        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />  
  14.    
  15.        <Setter Property="Template">  
  16.            <Setter.Value>  
  17.                <ControlTemplate TargetType="mui:ModernWindow">  
  18.                    <Border x:Name="WindowBorder" Margin="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}}">  
  19.                        <Border.Background>  
  20.                            <SolidColorBrush x:Name="WindowBorderBackground" Color="{DynamicResource WindowBackgroundColor}" />  
  21.                        </Border.Background>  
  22.                        <Border.Resources>  
  23.                            <Storyboard x:Key="BackgroundAnimation">  
  24.                                <ColorAnimation Storyboard.TargetName="WindowBorderBackground" Storyboard.TargetProperty="Color" To="{DynamicResource WindowBackgroundColor}" Duration="0:0:.6" />  
  25.                            </Storyboard>  
  26.                        </Border.Resources>  
  27.    
  28.                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">  
  29.                            <AdornerDecorator>  
  30.                                <Grid x:Name="LayoutRoot">  
  31.                                    <Grid.RowDefinitions>  
  32.                                        <RowDefinition Height="36" />  
  33.                                        <RowDefinition Height="Auto" />  
  34.                                        <RowDefinition Height="*" />  
  35.                                    </Grid.RowDefinitions>  
  36.    
  37.                                    <!-- window background content -->  
  38.                                    <ContentControl Grid.RowSpan="5" Content="{TemplateBinding BackgroundContent}" />  
  39.    
  40.                                    <!-- title bar -->  
  41.                                    <Grid>  
  42.                                        <Grid.ColumnDefinitions>  
  43.                                            <ColumnDefinition Width="*" />  
  44.                                            <ColumnDefinition Width="Auto" />  
  45.                                            <ColumnDefinition Width="Auto" />  
  46.                                            <ColumnDefinition Width="96" />  
  47.                                        </Grid.ColumnDefinitions>  
  48.    
  49.                                        <!-- title -->  
  50.                                        <TextBlock Text="{TemplateBinding Title}" Margin="8,0" VerticalAlignment="Center" Style="{StaticResource ModernWindowTitle}"  
  51.                                                   DataContext="{TemplateBinding IsTitleVisible}"  
  52.                                                   Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}}"/>  
  53.    
  54.                                        <!-- title links -->  
  55.                                        <ItemsControl Grid.Column="1" ItemsSource="{TemplateBinding TitleLinks}" Margin="0,0,24,0" WindowChrome.IsHitTestVisibleInChrome="True">  
  56.                                            <ItemsControl.ItemsPanel>  
  57.                                                <ItemsPanelTemplate>  
  58.                                                    <StackPanel Orientation="Horizontal" />  
  59.                                                </ItemsPanelTemplate>  
  60.                                            </ItemsControl.ItemsPanel>  
  61.                                            <ItemsControl.ItemTemplate>  
  62.                                                <DataTemplate>  
  63.                                                    <StackPanel Orientation="Horizontal">  
  64.                                                        <Line x:Name="Separator" X1=".5" Y1="3" X2=".5" Y2="12" Margin="5,0" VerticalAlignment="Center" Stroke="{DynamicResource SeparatorBackground}" />  
  65.    
  66.                                                        <Button Content="{Binding DisplayName}"  
  67.                                                                Command="mui:LinkCommands.NavigateLink"  
  68.                                                                CommandParameter="{Binding Source}"  
  69.                                                                CommandTarget="{Binding ElementName=ContentFrame}"  
  70.                                                                Style="{StaticResource SystemButtonLink}" />  
  71.                                                    </StackPanel>  
  72.                                                    <DataTemplate.Triggers>  
  73.                                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">  
  74.                                                            <Setter Property="Visibility" TargetName="Separator" Value="Collapsed"/>  
  75.                                                        </DataTrigger>  
  76.                                                    </DataTemplate.Triggers>  
  77.                                                </DataTemplate>  
  78.                                            </ItemsControl.ItemTemplate>  
  79.                                        </ItemsControl>  
  80.    
  81.                                        <!-- logo (visible only when LogoData is not null) -->  
  82.                                        <Border Grid.Column="2" Background="{DynamicResource Accent}" Width="36" Height="36" Margin="8,0"  
  83.                                                DataContext="{TemplateBinding LogoData}"  
  84.                                                Visibility="{Binding Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=inverse}">  
  85.                                            <Path Data="{Binding}" Stretch="Fill" Fill="White" Width="24" Height="24" HorizontalAlignment="Center" VerticalAlignment="Center" />  
  86.                                        </Border>  
  87.    
  88.                                        <!-- window system buttons-->  
  89.                                        <StackPanel Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Top" WindowChrome.IsHitTestVisibleInChrome="True">  
  90.                                            <Button Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}" ToolTip="{x:Static modernUi:Resources.Minimize}" Style="{StaticResource SystemButton}">  
  91.                                                <Button.Content>  
  92.                                                    <Grid Width="13" Height="12" RenderTransform="1,0,0,1,0,1">  
  93.                                                        <Path Data="M0,6 L8,6 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center"  
  94.                                                              Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2"  />  
  95.                                                    </Grid>  
  96.                                                </Button.Content>  
  97.                                            </Button>  
  98.                                            <Grid Margin="1,0,1,0">  
  99.                                                <Button x:Name="Restore" Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}" ToolTip="{x:Static modernUi:Resources.Restore}" Style="{StaticResource SystemButton}" Visibility="Collapsed" >  
  100.                                                    <Button.Content>  
  101.                                                        <Grid Width="13" Height="12" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">  
  102.                                                            <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"  
  103.                                                                  Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1"  />  
  104.                                                        </Grid>  
  105.                                                    </Button.Content>  
  106.                                                </Button>  
  107.                                                <Button x:Name="Maximize" Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}" ToolTip="{x:Static modernUi:Resources.Maximize}" Style="{StaticResource SystemButton}" >  
  108.                                                    <Button.Content>  
  109.                                                        <Grid Width="13" Height="12">  
  110.                                                            <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"  
  111.                                                                  Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2"  />  
  112.                                                        </Grid>  
  113.                                                    </Button.Content>  
  114.                                                </Button>  
  115.                                            </Grid>  
  116.                                            <Button Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}" ToolTip="{x:Static modernUi:Resources.Close}" Style="{StaticResource SystemCloseButton}" >  
  117.                                                <Button.Content>  
  118.                                                    <Grid Width="13" Height="12" RenderTransform="1,0,0,1,0,1">  
  119.                                                        <Path Data="M0,0 L8,7 M8,0 L0,7 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center"  
  120.                                                              Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1.5"  />  
  121.                                                    </Grid>  
  122.                                                </Button.Content>  
  123.                                            </Button>  
  124.                                        </StackPanel>  
  125.                                    </Grid>  
  126.    
  127.                                    <Grid Grid.Row="1">  
  128.                                        <Grid.ColumnDefinitions>  
  129.                                            <ColumnDefinition Width="42" />  
  130.                                            <ColumnDefinition Width="*"/>  
  131.                                        </Grid.ColumnDefinitions>  
  132.    
  133.                                        <!-- back button -->  
  134.                                        <!--<mui:ModernButton Margin="8,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"  
  135.                                                               EllipseDiameter="24" IconWidth="12" IconHeight="12"   
  136.                                                               IconData="F1 M 33,22L 33,26L 19.75,26L 27,33L 20.5,33L 11,24L 20.5,15L 27,15L 19.75,22L 33,22 Z"  
  137.                                                               Command="NavigationCommands.BrowseBack"  
  138.                                                               CommandTarget="{Binding ElementName=ContentFrame}"   
  139.                                                               ToolTip="{x:Static modernUi:Resources.Back}"  
  140.                                                               WindowChrome.IsHitTestVisibleInChrome="True" />-->  
  141.    
  142.                                        <!-- main menu -->  
  143.                                        <mui:ModernMenu Grid.Column="1"  
  144.                                                             SelectedSource="{Binding Source, ElementName=ContentFrame, Mode=TwoWay}"  
  145.                                                             LinkGroups="{TemplateBinding MenuLinkGroups}" />  
  146.                                    </Grid>  
  147.    
  148.                                    <!-- content frame -->  
  149.                                    <mui:ModernFrame x:Name="ContentFrame" Grid.Row="3" Grid.RowSpan="2" Margin="42,8,16,16" Source="{Binding ContentSource, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" ContentLoader="{TemplateBinding ContentLoader}"/>  
  150.    
  151.                                    <!-- resize grip -->  
  152.                                    <Grid Grid.Row="2" x:Name="ResizeGrip" Background="Transparent" Visibility="Collapsed" HorizontalAlignment="Right" VerticalAlignment="Bottom" WindowChrome.ResizeGripDirection="BottomRight">  
  153.                                        <Path Width="12" Height="12" Margin="1"  
  154.                                              Stroke="{DynamicResource WindowText}"  
  155.                                              StrokeThickness="1"  
  156.                                              Stretch="None"  
  157.                                              Data="F1 M1,10 L3,10 M5,10 L7,10 M9,10 L11,10 M2,9 L2,11 M6,9 L6,11 M10,9 L10,11 M5,6 L7,6 M9,6 L11,6 M6,5 L6,7 M10,5 L10,7 M9,2 L11,2 M10,1 L10,3" />  
  158.                                    </Grid>  
  159.                                </Grid>  
  160.                            </AdornerDecorator>  
  161.                        </Border>  
  162.                    </Border>  
  163.    
  164.                    <ControlTemplate.Triggers>  
  165.                        <Trigger Property="IsActive" Value="True">  
  166.                            <Setter Property="BorderBrush" Value="{DynamicResource WindowBorderActive}" />  
  167.                        </Trigger>  
  168.                        <Trigger Property="WindowState" Value="Maximized">  
  169.                            <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />  
  170.                            <Setter TargetName="Restore" Property="Visibility" Value="Visible" />  
  171.                            <Setter TargetName="LayoutRoot" Property="Margin" Value="7" />  
  172.                        </Trigger>  
  173.                        <Trigger Property="WindowState" Value="Normal">  
  174.                            <Setter TargetName="Maximize" Property="Visibility" Value="Visible" />  
  175.                            <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />  
  176.                        </Trigger>  
  177.                        <MultiTrigger>  
  178.                            <MultiTrigger.Conditions>  
  179.                                <Condition Property="ResizeMode" Value="CanResizeWithGrip" />  
  180.                                <Condition Property="WindowState" Value="Normal" />  
  181.                            </MultiTrigger.Conditions>  
  182.                            <Setter TargetName="ResizeGrip" Property="Visibility" Value="Visible" />  
  183.                        </MultiTrigger>  
  184.                    </ControlTemplate.Triggers>  
  185.                </ControlTemplate>  
  186.            </Setter.Value>  
  187.        </Setter>  
After that, we need to define this style in the MainWindow, such as the following:
  1. <mui:ModernWindow x:Class="ModernUIForWPFSample.WithoutBackButton.MainWindow"  
  2.                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.                   xmlns:mui="http://firstfloorsoftware.com/ModernUI"  
  5.                   Title="Modern UI without back button"  
  6.                   Width="650"  
  7.                   Height="550"  
  8.                   IsTitleVisible="True"  
  9.                   Style="{StaticResource MyModernWindow}">  
In App.xaml.cs the following resources must be added:
  1. <Application x:Class="ModernUIForWPFSample.WithoutBackButton.App"  
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.              StartupUri="MainWindow.xaml">  
  5.     <Application.Resources>  
  6.         <ResourceDictionary>  
  7.             <ResourceDictionary.MergedDictionaries>  
  8.                 <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />  
  9.                 <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml" />  
  10.                 <ResourceDictionary Source="Themes/ModernWindow.xaml" />  
  11.             </ResourceDictionary.MergedDictionaries>  
  12.         </ResourceDictionary>  
  13.     </Application.Resources>  
  14. </Application>  
Here, in App.xaml, the difference from the Default Windows sample is that we added the new resource dictionary that contains the style we defined.

When we run the application we got something like the following.

modern ui

Note: the preceding image has the entire procedure. To define the theme color for the window, we need to define the color in the constructor, by doing:
  1. AppearanceManager.Current.AccentColor = Colors.DarkViolet;  
To select the first view that will show we need to do something like:
  1. ContentSource = MenuLinkGroups.First().Links.First().Source;  
Source Code

Get the source code for this sample in github.

Visual Studio Extension used
logo

<< Modern UI for WPF Application by Example (Default Window)          Modern UI for WPF Application by Example (Blank Window)>>