Page view counter

Styles and Templates

Follow up posted at bottom of article

I’m about to release a tutorial and a few videos on Styles and Templates. To set the stage, I wanted to discuss one aspect of templating in Silverlight that is in some ways revolutionary.

The designers of the Silverlight 2 control libraries established from the outset that there would be an absolute decoupling of the control logic (as implemented by event handlers) from the User Interface. This allows the UI to be dictated entirely by Tempolates, (and the Visual State Machine) and in turn that allows templates to be written entirely in Xaml and to consist of a combination of graphic primitives and story boards (transitions).

This means that every control is lookless

 

The appearance of all the controls in the toolbox is just the appearance provided by the “out of the box” template assigned to it  by the control creator. It has no more intrinsic validity than any other skin you might put on the control.

Say what?

That means you are absolutely and 100% free to use the button as is, or to make it look like a dog, and have it bark when pressed. Or, consider the images shown here. Both sets show buttons being before and after being clicked. Other than aesthetics and convention, there is nothing that makes one set more correct than the other.

Button1Unclicked ButtonClick1

 Button2UnclickedButtonClick2

The Possible States are Defined and Embedded in Metadata

Both “looks” are equally valid, and both buttons send exactly the same click event.  Since the designer of the out of the box button was kind enough to embed all the button’s state information in the button’s meta-data (e.g., “I have the states Normal, MouseOver, MouseDown, Disabled, Focus and NotFocused), and since Expression Blend reads that meta data and makes editing the appearance and behavior astonishingly easy, it is not only valid to change it to a round, spinning,  dipping shape,  it is (to be honest) fun.

Three Ways To Modify Controls

 

As you will see in the tutorial to be release within the next day or so, I distinguish among three ways to modify the look of a control:

1. “In line” attributes

2. Style objects

3. Templates

In Line Attributes

Very briefly, the first of these we all do all the time. This is as simple as adding such UI controlling attributes as HorizontalAlignment="Left" to the Xaml.  It is fielder’s choice as to whether you set these attributes directly in Xaml or let Blend set them for you as you create your UI

Styles

The second is a more formal approach in which you create a Style object that can be applied to multiple controls to provide a uniform look to controls used in the same way. The cannonical example is TextBlocks used as labels.

You might decide that all labels in your application will be

  • Foreground=”Black”
  • FontFamily=”Verdana”
  • FontSize=”18”
  • VerticalAlignment=”Bottom”
  • HorizontalAlignment=”Right”
  • Margin=”10,0,15,0”

Rather than having to repeat this for every TextBlock you can create a style (in the .xaml page or for the entire application in App.xaml) and then apply the style to each TextBlock used as a label like this:

<TextBlock x:Name="ProgMtl" Text="Pregressive Metal"
Grid.Row="2" Grid.Column="0" Style="{StaticResource LabelStyle}" />

<TextBlock x:Name="JazzLbl" Text="Jazz"
Grid.Row="2" Grid.Column="0" Style="{StaticResource LabelStyle}" />

 

 

In previous documents I showed how to set Styles by hand, but Blend makes this so easy and so much less error prone I’ve stopped hand-coding Styles.

Tempaltes

Templates is the big enchelada. With the advent of the Visual state Machine, and the use of storyboards for transitions between states (giving a much more natural look to your application)  templates simultaneously free you to create the most dramatic and impressive changes to your application while virtually (but not literally) forcing you to use a tool like Blend to do so (you can hand-code anything if you are patient enough).

Much more on the details of Templates to come.

Follow-up


More
Templates and Custom Controls

Published 05 July 2008 08:36 AM by jesseliberty

Comments

# Dew Drop - July 6, 2008 | Alvin Ashcraft's Morning Dew said on 06 July, 2008 08:16 AM

Pingback from  Dew Drop - July 6, 2008 | Alvin Ashcraft's Morning Dew

# Radiox said on 06 July, 2008 04:22 PM

Hi Jesse,

There are 2 misspelled 'templates' words in this post. Search for 'tempolates' and 'tempaltes'. And delete this comment because is not needed here.

Regards,

# Maciek said on 06 July, 2008 06:21 PM

Hi Jesse,

Do your upcoming tutorials cover the topic of : "Creating a template-able control from scratch" ? I was trying to find some info on that but didn't have much luck.

Regards

# wpfauthor said on 06 July, 2008 09:01 PM

Hello Jesse, in my book Foundation Blend 2 I show how to build a custom Button Control using ControlTemplates.  I am pretty certain it will teach you everything you are wanting to learn about ControlTemplates.  

The URL on Amazon is:

www.amazon.com/.../ref=pd_bbs_sr_1

Hope this helps you out.  Victor Gaudioso

# wpfauthor said on 06 July, 2008 09:04 PM

I apologize, that comment was made for Maciek.  Jesse, I also wanted to say that your definition of Styles and Templates are excellent!

# jesseliberty said on 06 July, 2008 10:05 PM

Maciek,

Yes, we have tutorials and videos on the way (but not immediately) about building custom controls and doing so with templating, including adding the metadata necessary for both the VSM and for Blend.

# jesseliberty said on 06 July, 2008 10:23 PM

Victor,

Normally, I don't comment on other author's work, but since you placed your comment (and link to your book) here, I have to take respectful issue with some of what you said.  

Your book shows how to create a wpf custom control, but not how to create a templatable Silverlight custom control as far as I can tell.

You use the term custom control and user control interchangeably but my understanding is that in the new VSM/parts model, if you create a custom user control it will not support templates; that to make a skinnable custom control you must inherit from the Control class and use a ControlTemplate.

Maciek asked about creating a Templatable custom control from scratch and you said "I show how to build a custom Button Control using ControlTemplates.  I am pretty certain it will teach you everything you are wanting to learn about ControlTemplates."

Again, I like your book, I bought your book, and I don't run down other author's books, but since you wrote that here, I don't see where you cover the parts model or the VSM or what it takes to create a Silverlight templatable custom control at all.

You show how to set triggers, which are not used in Silverlight, but not how to set TemplateVisualState attributes, which are essential to what he needs.

I'm honestly not trying to give you a hard time, but I think Silverlight is different, and you are setting folks up for some disappoitment by conflating WPF custom controls and Silverlight templatable custom controls.

I apologize for sounding so harsh, but it is important that this new model be properly understood as folks move forward.

Maciek: This has come up often enough that I will endeavor to make it the next tutorial I write (along with a few videos) which should have it ready by the end of the month or the beginning of August.

Thanks.

# jesseliberty said on 07 July, 2008 12:10 AM

This discussion reminded me of how urgent this subject is, so not only am I moving up the "creating skinnable custom controls" tutorial and videos in the schedule, but I've been able to swap in a live webcast on the topic as well. Please see the revised schedule link in the sidebar under presentations.

Thanks!

-j

# Community Blogs said on 07 July, 2008 12:18 AM

Michael Washington on SL Security, Jesse LIberty on Styles &amp; Templates, Koen Swikstra on Tweening

# Maciek said on 07 July, 2008 04:02 AM

Victor : Thanks for the comment but - as Jesse pointed out - I wasn't looking for buttons specifically - it's more about creating something truly custom. For instance I've seen a weather control lately - which was using templating and was scratch built - I didn't get a chance to read it's code fully though.

Regards.

# wpfauthor said on 07 July, 2008 07:15 PM

Hello Jesse,

After reading your post I went back to the chapter on ControlTemplates, Styles and Custom UserControls.  And you are correct, it does show how to build WPF UserControls and not SL.  I thought I showed how to do it in both WPF and SL but I guess my memory is not so good :).  I apologize, I guess I should be a little more careful when speaking on such matters.  

Maciek, are you doing WPF development as well?  If so, I can make it up to you by sending you free signed copy.  Let me know.

Looking forward to the video tutorials Jesse; great job and thanks for reading my book.

# wpfauthor said on 07 July, 2008 09:15 PM

Jesse,

After your post I went and tried to make a SL 2 UserControl of an Image Viewer that uses the VSM to show the image.  I was able to successfuly create the UC but I was not able to tell it to use a Template. Any idea of what I am missing.  You can see the Image Viewer here:

www.windowspresentationfoundation.com/.../Default.html

If you would like the source it is located here:

windowspresentationfoundation.com/.../SL_ImageStateUC.zip

Now I really cannot wait for your video tutorials on this subject.

# jesseliberty said on 07 July, 2008 10:19 PM

That is a really nice image viewer. I probably won't have time to look at your code before my webcast on Wednesday, but I have three videos coming out on styles and templates any day now. If that doesn't help, I'd be happy to figure out the specific issue and write something up or otherwise try to figure it out.

# Maciek said on 08 July, 2008 12:14 PM

Hmmm not sure what happened to my last comment so I'll answer again :)

Yeah, I do dabble in WPF as well, having a copy of your book would be lovely.

# wpfauthor said on 08 July, 2008 01:40 PM

Jesse,

Thanks!  Yeah, don't worry about it, I will just wait for the videos.

Maciek,

Email me where you want the book sent at wpfauthor at g mail dot com and I will send it out.

Thanks, Victor

# Jesse Liberty - Silverlight Geek said on 08 July, 2008 03:54 PM

I’m very pleased to announce that my latest tutorial, on Styles, Templates and the Visual State Manager

# Mirrored Blogs said on 08 July, 2008 04:04 PM

I’m very pleased to announce that my latest tutorial, on Styles, Templates and the Visual State Manager

# Microsoft Weblogs said on 08 July, 2008 04:30 PM

I’m very pleased to announce that my latest tutorial, on Styles, Templates and the Visual State Manager

# Mirrored Blogs said on 08 July, 2008 06:21 PM

Post: Approved at: Jul-8-2008 GUI .. GUI ... GUI ... Themes for Silverlight Applications &quot;A theming

# Jesse Liberty - Silverlight Geek said on 22 December, 2008 05:34 PM

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

# Microsoft Weblogs said on 22 December, 2008 06:17 PM

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

# Templates & Custum Controls at Programming with Silverlight, WPF & .NET said on 23 December, 2008 08:29 AM

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Templates & Custum Controls at Programming with Silverlight, WPF & .NET said on 23 December, 2008 08:31 AM

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Jesse Liberty - Silverlight Geek said on 31 December, 2008 04:10 PM

One way that many developers like to create styles and templates for controls is to modify the existing

# Santiago Palladino » Silverlight Xaml Guidelines said on 02 March, 2009 11:28 AM

Pingback from  Santiago Palladino &raquo; Silverlight Xaml Guidelines

# said on 18 March, 2009 03:35 PM

As of this morning I have uploaded five videos (with C# and VB source code) for Silverlight 3.&#160;

# Microsoft Weblogs said on 18 March, 2009 04:20 PM

As of this morning I have uploaded five videos (with C# and VB source code) for Silverlight 3. To have

# bradutz01 said on 24 April, 2009 07:26 AM

Hi,

At www.xamltemplates.net/sl you can find a complete theme for all the silverlight controls, check it out.