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 Saturday, July 05, 2008 8:36 AM by jesseliberty

Comments

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

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

# re: Styles and Templates

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,

Sunday, July 06, 2008 4:22 PM by Radiox

# re: Styles and Templates

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

Sunday, July 06, 2008 6:21 PM by Maciek

# re: Styles and Templates

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

Sunday, July 06, 2008 9:01 PM by wpfauthor

# re: Styles and Templates

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

Sunday, July 06, 2008 9:04 PM by wpfauthor

# re: Styles and Templates

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.

Sunday, July 06, 2008 10:05 PM by jesseliberty

# re: Styles and Templates

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.

Sunday, July 06, 2008 10:23 PM by jesseliberty

# re: Styles and Templates

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

Monday, July 07, 2008 12:10 AM by jesseliberty

# Silverlight Cream for July 6, 2008 -- #318

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

Monday, July 07, 2008 12:18 AM by Community Blogs

# re: Styles and Templates

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.

Monday, July 07, 2008 4:02 AM by Maciek

# re: Styles and Templates

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.

Monday, July 07, 2008 7:15 PM by wpfauthor

# re: Styles and Templates

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.

Monday, July 07, 2008 9:15 PM by wpfauthor

# re: Styles and Templates

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.

Monday, July 07, 2008 10:19 PM by jesseliberty

# re: Styles and Templates

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.

Tuesday, July 08, 2008 12:14 PM by Maciek

# re: Styles and Templates

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

Tuesday, July 08, 2008 1:40 PM by wpfauthor

# Tutorial on Styles & Templates Now Available

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

Tuesday, July 08, 2008 3:54 PM by Jesse Liberty - Silverlight Geek

# Tutorial on Styles & Templates Now Available

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

Tuesday, July 08, 2008 4:04 PM by Mirrored Blogs

# Tutorial on Styles & Templates Now Available

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

Tuesday, July 08, 2008 4:30 PM by Microsoft Weblogs

# Post: 137

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

Tuesday, July 08, 2008 6:21 PM by Mirrored Blogs

# Application Set: Templates & Custom Controls

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

Monday, December 22, 2008 5:34 PM by Jesse Liberty - Silverlight Geek

# Application Set: Templates & Custom Controls

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

Monday, December 22, 2008 6:17 PM by Microsoft Weblogs

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

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

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

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

# Template Browser – an indispensible tool

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

Wednesday, December 31, 2008 4:10 PM by Jesse Liberty - Silverlight Geek

# Santiago Palladino &raquo; Silverlight Xaml Guidelines

Pingback from  Santiago Palladino &raquo; Silverlight Xaml Guidelines

Monday, March 02, 2009 11:28 AM by Santiago Palladino » Silverlight Xaml Guidelines

# Compiling the Silverlight 3 Video’s Code

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

Wednesday, March 18, 2009 3:35 PM by

# Compiling the Silverlight 3 Video’s Code

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

Wednesday, March 18, 2009 4:20 PM by Microsoft Weblogs

# re: Styles and Templates

Hi,

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

Friday, April 24, 2009 7:26 AM by bradutz01

# Creating A Form – Level 100

(click on the image to see other Mini-tutorials ) TURING Entry [100:4] [ What are these numbers? ] In

Tuesday, August 04, 2009 4:56 PM by Microsoft Weblogs