I’m a bit of a React.js fan lately, and with the arrival of Google’s Material Design Lite, I decided to combine them for a little project I’m throwing together. Turns out to be pretty easy. To simplify the Getting Started, you need to include a .css and .js file in your page:
The small gotcha for using templating techniques for dynamic pages such as with React.js is that for buttons, and other items, dynamically added you need to call a bit of script to upgrade the button:
//where button is your DOM element
componentHandler.upgradeElement(button, 'MaterialButton');
But when we come to create our React class, that’s easily handled in componentDidMount, leaving my Material Design Lite button class looking like this:
One small thing to note is the second arg to upgradeElement where we are passing in ‘MaterialButton’. As of now the docs aren’t overly clear on this, but it’s the name of the Javascript function object defined in the source. So, if we want to create a check box React class we can dig in to the Material Design Light source code in GitHub, and confirm that we’d pass ‘MaterialCheckbox’ into upgradeElement.
No doubt the community will provide a full library for this in time, but it doesn’t take much to combine these two libraries.
Open source doesn’t have to just apply to code. I’ve been applying my XAML skills to Material Design In XAML Toolkit to help other developers quickly craft good looking user interfaces in WPF. I’m pretty happy with the results, and in some of my own projects I’ve produced some striking applications using the toolkit. Despite doing this, I readily admit I’m no designer. For such a visually focused code library I really wanted a logo, but knew I could never do the task justice.
So I threw the task out to the community.
I created an issue on GitHub, tweeted a bit, posted on Reddit. And waited. Pretty much bang on 2 weeks later a result came back from a young guy called Sam, and I’m really happy with it:
Sam or “snalty” can be found on Twitter, and you can see some of his other designs on his blog.
In summary, a great bit of collaboration helping to push Material Design In XAML Toolkit further along.
But the fun part was cranking a couple of brand new controls to build the Material Design time picker experience:
TimePicker
Clock
These are sibling controls to the existing DatePicker and Calendar controls. I wanted to keep the API experience similar so you can dive straight in without any kind of learning curve. The Clock can be used in isolation, or use the DatePicker for an easy picker/popup/drop down behaviour.
Here’s a static glimpse at the picker:
And here’s a gif of the clock in action:
There’s nothing complicated about using these, but you will need to get Material Design In XAML Toolkit referenced and set up in your app. Follow the initial tutorial, and head over to GitHub to download the source/examples project.
Material Design templates for DatePicker/Calendar primitive controls.
Please note that the behaviour is not quite identical to that which you might see on Android. These are templates for the existing WPF DatePicker and Calendar primitive controls, and thus, some of the behaviour around month/year selection is driven by the nature of those controls. There is no modal dialog, as I don’t want these templates to get confused with any requirement/API used to manage dialogs in WPF.
I must say that I believe that it is testament to the original design of WPF/XAML – which is now a mature technology – that I could slot these styles in pretty much seamlessly for a control whose code base is around 8 years old. Note that some extra code has gone in to the Material Design Toolkit, bit this is wrapped inside the new templates and thus if you consume these templates you need never concern yourself with it.
In this post I will demonstrate how to – very quickly – combine Dragablz and MaterialDesignColors in WPF to create a great looking control which supports full tear out and can use the Google Material Design colour palette.
Dragablz Tab Contrtol and Material Design
Start a new WPF project. We rely on two NuGet packages, so get them installed straight away. Install from the Package Manager tool in Visual Studio, or, from the NuGet console run these commands:
Already if you run this project you will have a tab control that supports Chrome-style tearing out of tabs. But it wont 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 up your App.xaml. We have to merge in three dictionaries. The first two are to set up your 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 full App.xaml is below:
<Application x:Class="MaterialDesignColors.WpfExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- primary color -->
<ResourceDictionary>
<!-- include your primary palette -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Indigo.xaml" />
</ResourceDictionary.MergedDictionaries>
<!--
include three hues from the primary palette (and the associated forecolours).
Do not rename, keep in sequence; light to dark.
-->
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource Primary500}"/>
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary700}"/>
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary700Foreground}"/>
</ResourceDictionary>
<!-- secondary colour -->
<ResourceDictionary>
<!-- include your secondary pallette -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Yellow.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- include a single secondary accent color (and the associated forecolour) -->
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent200}"/>
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent200Foreground}"/>
</ResourceDictionary>
<!-- Include the Dragablz Material Design style -->
<ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- tell Dragablz tab control to use the Material Design theme -->
<Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" />
</ResourceDictionary>
</Application.Resources>
</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 which are referenced. You can also tweak the hues, but do not change the brush names. Dragablz will be looking for these.