spacer
 
  • What we do
  • Sharing
  • Jobs
  • About us
← Using custom OTF fonts in Windows 8 XAML A Few Good Reasons for Code Review →

Styling windows 8 – 3 Implicit and explicit styling

by Håkan Reis - .Net, User Experience

spacer

Now your app is getting some style, we got the colors nailed down and now we also got a workflow for how fonts and text styles should be handled in your development resources. Let’s continue with control styling. It’s time to focus on updating the controls with explicit and implicit styling. The Index so far:

Styling windows 8 – 1 Colors and themes
Styling windows 8 – 2 Text and font styles
Styling windows 8 – 3 Implicit and explicit styling
Styling windows 8 – 4 The button

Styling a control … and the controls control

Simply put explicit styling means you create a style, and when you want to use it you reference the styling one each control that should use it.

When you start out editing a control's style, often it’s easier to start with a copy of the default style for the control. Say you have a slider and want to change the size of the grip in that slider. The first thing you do is place the slider and edit the template where you can create a copy of the original style:

spacer

Notice how the slider already have gotten the colors from our color theme we defined.

This will fire up a dialog where you can name your style and where you want to place it.

With a copy of the system style for the slider,  it’s time to start styling. The first thing you notice is that there are actually two styles, the horizontal and the vertical slider style. If this was just a single styling that should not be reused I’d get rid of the vertical style altogether. I hate unused code… However, this time we are aiming at a style that should be standard for all sliders in the application, so we better take care of both horizontal and vertical.

The thumbs size is controlled by a system resource called SliderTrackThemeHeight, so if we want to set a new size we have to convert that to a new local resource. Click on the little green box next to width or height of the HorizontalThumb and select Convert to new resource… Name it something smart like BigSliderTrackThemeSize. Now click on the little green box again but this time select edit resource and set it to 24. Make sure both the height and width use this new local resource. And don’t forget to change that on the vertical slider as well.

There, a bigger thumb. But also bigger tracker (the gray area) and decrease (the green part). Lets fix those by setting a top and bottom margin for those.  For the vertical slider that should be left and right margins.

That should do it, we now have a bigger thumb marker in our style. Now let’s add a new slider and see how it looks? Well, as this is an explicit style the new slider doesn't automatically get the modified style. We have to set this explicitly. Like this:

spacer

So the last thing for this styling. If you wanted to add something more to the slider thumb how do I do that? This is where the controls of the control comes in. If you edit the style you notice that there are a few parts that have their own templates. One of those are the HorizontalThumb (and the VerticalThumb) both of these are using the same template that are in the system templates. So if you want to change things here it’s the same procedure as the for the slider itself. For this demo I created HorizontalSliderThumbStyle and VerticalSliderThumbStyle that adds a hang to the slider so it’s outside the tracker and crease.

Parental Advisory: Implicit styles

The style for the sliders are now in place, what if I want to use it outside of this page? That one is easy, just make sure you move the resources for the styles from the page level and up to application level. Easiest way is to drag and drop the styles that’s needed to the resource in ThemeStyles.xaml. That way all references to the styles are kept intact.

spacer

There, now you can go on and use this style with any page in the application. Just set the style on each slider to BigThumbSliderStyle.

Hey, this is a great looking slider, what if I forget to set it when I add a new slider? Wouldn't it be easier if I just tell the application to always use that style instead? You are absolutely right, so let us do that.

That’s where implicit styling comes in, and it’s dead easy. If we go into the xaml code of the style (go into resources, right click on BigThumbSliderStyle, and select View Source and bam, you are there) you will see the following entry:

<Style x:Key="BigThumbSliderStyle" TargetType="Slider">
  <Setter Property="Background" Value="{StaticResource SliderTrackBackgroundThemeBrush}"/>
  <Setter Property="BorderBrush" Value="{StaticResource SliderBorderThemeBrush}"/>

This is the beginning of the slider style (see the target type: Slider) if we just go in and remove the style key we then have an implicit style. That’s it! Whenever you create a new slider this style will be used. Neat. It even shows in the resource list: [Slider default].

Last thing to do is remove the Style="{StaticResource BigThumbSliderStyle}" from the sliders. Currently we can’t run the application as we have a couple of "The resource "BigThumbSliderStyle" could not be resolved" errors right now. The Blend view won’t crash though, if the key is missing it will drop back to the default view for the slider and we know what that one is.

A final note on this: It may seem strange to first designing on the page level when you know it’s going to be an implicit style in the end. however, it is way easier to design in the page. Here the data and the slider is shown in context as it will be used by your application. you can even see how it connects to sample data if you have your view model set up right. If you edit the parts in your resources directly you just see small parts out of context. Moving the finished style out to the right resource and the key is so much easier.

The source for Implicit&Explicit styling

Next I’ll dig in to one of my favorite controllers: the button.

  • metro
  • Win8
  • windows 8
  • windows store
  • xaml

7 Responses to “Styling windows 8 – 3 Implicit and explicit styling”

  1. spacer Roger says:
    December 26, 2012 at 14:38

    I hope that developers will identify if they are talking about the phone or the PC when stating they have information on how to edit settings. This site does not do that and it is misleading.

    Reply
    • spacer Håkan Reis says:
      December 26, 2012 at 16:55

      As the title says its windows 8

      Reply
  2. Styling windows 8 – 4 The button – Jayway says:
    November 27, 2012 at 18:01

    [...] windows 8 – 1 Colors and themes Styling windows 8 – 2 Text and font styles Styling windows 8 – 3 Implicit and explicit styling Styling windows 8 – 4 The [...]

    Reply
  3. Styling Windows 8 – 3 Implicit and explicit styling | Answer My Query says:
    November 7, 2012 at 13:02

    [...] Styling Windows 8 – 3 Implicit and explicit styling by SilverlightShow: Silverlight Community under – Miscelleneous Read original post by Hakan Reis at JayWay Blog [...]

    Reply
  4. Styling windows 8 – 1 Colors and themes – Jayway says:
    November 7, 2012 at 11:00

    [...] Styling windows 8 – 3 Implicit and explicit styling [...]

    Reply
  5. Styling windows 8 – 2 Text and font styles – Jayway says:
    November 7, 2012 at 11:00

    [...] Styling windows 8 – 3 Implicit and explicit styling [...]

    Reply
  6. Windows Store Developer Links – 2012-11-07 | Dan Rigby says:
    November 7, 2012 at 01:29

    [...] how to add “About” and “Privacy Policy” for your application…”Styling windows 8 – 3 Implicit and explicit styling (Håkan Reis)“Let’s continue with control styling. It’s time to focus on updating [...]

    Reply

Leave a Reply

Click here to cancel reply.

Subscribe to this site's feed

We are eager to meet talented people

» Check out current openings

Categories

  • .Net
  • Agile
  • Android
  • Architecture
  • Cloud
  • Cocoa
  • DevOps
  • Dynamic languages
  • Embedded
  • Events
  • Java
  • Ruby
  • Testing
  • Tips & Tricks
  • Uncategorized
  • User Experience
  • Web

Tags

.Net Android aop automated testing azure clojure concurrency conference design patterns easymock frameworks git iphone Java java me javascript jayview junit linux mac maven metro mobile mock objective-c open source oredev performance powermock programming rest Ruby scala silverlight spring testing tools tutorial ubuntu web windows 8 windows phone windows phone 7 wp7dev xaml

Archives

  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • July 2008
  • June 2008
  • May 2008
  • March 2008
  • February 2008
  • September 2007
  • May 2007
  • February 2007
  • January 2007
  • December 2006
  • October 2006
  • September 2006
  • August 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • August 2005
  • March 2005
  • August 2004
  • February 2004
  • January 2004
  • August 2001
  • May 2001
  • March 2001
  • January 2001
  • November 2000

Authors

  • Adam Skogman (2)
  • Albin Theander (1)
  • Amir Moulavi (7)
  • Anders Bursjöö (1)
  • Anders Davoust (1)
  • Anders Ericsson (5)
  • Anders Eriksson (2)
  • Anders Fjeldstad (1)
  • Anders Göransson (4)
  • Anders Janmyr (42)
  • Anders Poulsen (10)
  • Andreas Hallberg (6)
  • Andreas Hammar (40)
  • Andreas Nilsson (1)
  • Andreas Ronge (5)
  • Björn Antonsson (1)
  • Björn Carlsson (6)
  • Björn Granvik (29)
  • Christian Hedin (4)
  • Christian Jacobsen (5)
  • Daniel Kleveros (2)
  • Darius Katz (3)
  • Davor Crnomat (4)
  • Erik Ogenvik (2)
  • Ester Ytterbrink (1)
  • Fjodor Ekström (1)
  • Fredrik Olsson (31)
  • Gustaf Nilsson Kotte (10)
  • Håkan Reis (26)
  • Henrik Andersson (3)
  • Henrik Bernström (5)
  • Henrik Feldt (5)
  • Henrik Larne (2)
  • Henrik Lundahl (1)
  • Hugo Josefson (2)
  • Jacob Mattsson (2)
  • Jakob Nilsson-Ehle (3)
  • Jakob Wolman (3)
  • Jan Kronquist (21)
  • Jan-Olof Eriksson (1)
  • Jens Nordahl (1)
  • Jimmy Falkbjer (1)
  • Joakim Back (1)
  • Joakim Hogart (1)
  • Johan Haleby (24)
  • Johan Karlsson (3)
  • Johan Lundahl (3)
  • Johan Olsson (3)
  • Johan Rask (4)
  • Johan Silfversparre (3)
  • Karin Hofbauer (1)
  • Kenneth Andersson (1)
  • Lars-Håkan Jönsson (1)
  • László Urszuly (1)
  • Mads Enevoldsen (6)
  • Mads Troest (2)
  • Magnus Mårtensson (9)
  • Magnus Palmér (1)
  • Magnus Robertsson (3)
  • Magnus Rydin (1)
  • Mario Boikov (3)
  • Mårten Österberg (1)
  • Mattias Ask (8)
  • Mattias Hellborg Arthursson (12)
  • Mattias Lindskog (5)
  • Mattias Rosberg (3)
  • Mattias Severson (13)
  • Mattias Sjögren (1)
  • Michael Kober (5)
  • Mikael Karlsson (1)
  • Morten Faester (1)
  • Niklas Lundberg (1)
  • Niklas Uhrberg (2)
  • Nino Martinez (1)
  • Olof Åkesson (2)
  • Oskar Wickström (1)
  • Pär Sikö (1)
  • Patrik Nordwall (1)
  • Per Böckman (2)
  • Per Ökvist (13)
  • Per Wendel (1)
  • Per-Erik Bergman (9)
  • Per-Olof Bondesson (2)
  • Peter Neubauer (7)
  • Pete
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.