Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

Styles and Templates

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.

Comments

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

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

# July 6, 2008 8:16 AM

Radiox said:

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,

# July 6, 2008 4:22 PM

Maciek said:

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

# July 6, 2008 6:21 PM

wpfauthor said:

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

# July 6, 2008 9:01 PM

wpfauthor said:

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

# July 6, 2008 9:04 PM

jesseliberty said:

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.

# July 6, 2008 10:05 PM

jesseliberty said:

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.

# July 6, 2008 10:23 PM

jesseliberty said:

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

# July 7, 2008 12:10 AM

Community Blogs said:

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

# July 7, 2008 12:18 AM

Maciek said:

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.

# July 7, 2008 4:02 AM

wpfauthor said:

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.

# July 7, 2008 7:15 PM

wpfauthor said:

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.

# July 7, 2008 9:15 PM

jesseliberty said:

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.

# July 7, 2008 10:19 PM

Maciek said:

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.

# July 8, 2008 12:14 PM

wpfauthor said:

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

# July 8, 2008 1:40 PM

Jesse Liberty - Silverlight Geek said:

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

# July 8, 2008 3:54 PM

Mirrored Blogs said:

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

# July 8, 2008 4:04 PM

Microsoft Weblogs said:

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

# July 8, 2008 4:30 PM

Mirrored Blogs said:

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

# July 8, 2008 6:21 PM