How to Use the Material Design Theme With Dragablz Tab Control

Combining Google Material theme, Design and Dragablz for a great, tear-able TabControl.

In this article I will show how to very quickly combine Dragablz and MaterialDesignColors in WPF to create a great looking control supporting full tear-out and can use the Google Material Design colour palette.

MaterialDesignDemo1

Start a new WPF project. We rely on two NuGet packages, so get them installed straight away. Install them from the Package Manager tool in Visual Studio, or from the NuGet console, run these commands:

  • Install-Package Dragablz
  • Install-Package MaterialDesignColors

In the MainWindow.xaml, setup a simple usage of the Dragablz TabablzControl:

  1. <Window x:Class="MaterialDesignTabExample.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"    
  5.         Title="Material Design Demo" Height="350" Width="525">    
  6.     <dragablz:TabablzControl>    
  7.         <dragablz:TabablzControl.InterTabController>    
  8.             <dragablz:InterTabController />    
  9.         </dragablz:TabablzControl.InterTabController>    
  10.         <TabItem Header="HELLO">    
  11.             <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Hello World</TextBlock>    
  12.         </TabItem>    
  13.         <TabItem Header="MATERIAL">    
  14.             <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Material Design</TextBlock>    
  15.         </TabItem>    
  16.         <TabItem Header="DESIGN">    
  17.             <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Looks Quite Nice</TextBlock>    
  18.         </TabItem>    
  19.     </dragablz:TabablzControl>    
  20. </Window>   
Already if you run this project you will have a tab control that supports Chrome-style tearing out of tabs. But it won't look too good. So, the next step is to bring in the Material Design colours and tell Dragablz to use the Material Design style.

Open your App.xaml. We need to merge in three dictionaries. The first two are to set up the Material Design colour palette. The MaterialDesignColors assembly contains a ResourceDictionary for each color (a collection of hues and accents). To create a full palette we need to bring in a primary colour, set up some hue brushes and then bring in a secondary color for our accent color. The third resource dictionary is to include the Dragablz theme for Material Design. Finally we instruct our tab control to use the correct style.

Don't worry, it's not too complicated. The following is the full App.xaml:
  1. <Application x:Class="MaterialDesignColors.WpfExample.App"    
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.              xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"    
  5.              StartupUri="MainWindow.xaml">    
  6.     <Application.Resources>    
  7.         <ResourceDictionary>    
  8.             <ResourceDictionary.MergedDictionaries>    
  9.                 <!-- primary color -->    
  10.                 <ResourceDictionary>    
  11.                     <!-- include your primary palette -->    
  12.                     <ResourceDictionary.MergedDictionaries>    
  13.                         <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Indigo.xaml" />    
  14.                     </ResourceDictionary.MergedDictionaries>    
  15.                     <!--    
  16.                             include three hues from the primary palette (and the associated forecolours).    
  17.                             Do not rename, keep in sequence; light to dark.    
  18.                         -->    
  19.                     <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>    
  20.                     <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>    
  21.                     <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource Primary500}"/>    
  22.                     <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>    
  23.                     <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary700}"/>    
  24.                     <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary700Foreground}"/>    
  25.                 </ResourceDictionary>    
  26.     
  27.                 <!-- secondary colour -->    
  28.                 <ResourceDictionary>    
  29.                     <!-- include your secondary pallette -->    
  30.                     <ResourceDictionary.MergedDictionaries>    
  31.                         <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Yellow.xaml" />    
  32.                     </ResourceDictionary.MergedDictionaries>    
  33.     
  34.                     <!-- include a single secondary accent color (and the associated forecolour) -->    
  35.                     <SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent200}"/>    
  36.                     <SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent200Foreground}"/>    
  37.                 </ResourceDictionary>    
  38.     
  39.                 <!-- Include the Dragablz Material Design style -->    
  40.                 <ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml"/>                    
  41.     
  42.             </ResourceDictionary.MergedDictionaries>    
  43.     
  44.             <!-- tell Dragablz tab control to use the Material Design theme -->    
  45.             <Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" />    
  46.         </ResourceDictionary>    
  47.     </Application.Resources>    
  48. </Application>  
And that's it. Fire up your baby and you are done. You can change the colours by changing the two colour resource dictionaries that are referenced. You can also tweak the hues, but do not change the brush names. Dragablz will be looking for these.
The fun doesn't stop here, in my next article, Material Design In XAML - Mash Up! I explain how to combine this stuff with Material Design in XAML Tookit and MahApps to create an even better looking application.
 
Links: Enjoy!