I Like the Silverlight 2.0 Control Templating Model (better than WPF's)

There have been a lot of opinions flying around the web-o-net about the problems with the control templating model in Silverlight 2.0.  I’m going to go out on a limb here, though, and say…I like it.  That’s right, I like the new model.  In fact (insert teeth gritting here), I wish we had done the same thing in WPF (gasp!).

The reasonable opinion against the new model (as I understand it) is based on the fact that Silverlight doesn’t have support for triggers in control templates (or just about anywhere for that matter).  This means more code and less XAML for defining those behaviors. 

In WPF, for example, if you wanted a control to respond to a mouse hover (and, say, change its color), you would create a trigger on the IsMouseOver property and then party in that trigger and create whatever storyboards (or setters) you want to implement that color change.  This all happens in markup. 

In Silverlight, on the other hand, the developer has to create an event handler for the MouseEnter and MouseLeave events and then explicitly load a set of storyboards with well known names to handle the transitions between those states.  Silverlight is doing a lot less heavy lifting for you and there’s definitely a code component to what, in WPF, seemed like a simple XAML task.

See how much better Silverlight is?  Okay, so the "better" part may not be obvious.  Here’s my take though:

First, things got much cleaner in the new component model.  Do you remember this doc?  It’s an attempt to prioritize the eight (that’s right, eight) different "best practices" for expressing control state changes and behavior in your control.  This includes: bindings, triggers, commands, named parts, etc.  This is some serious complexity and leads to some serious lack of discoverability.  This is why we needed projects like SimpleStyles.  Templating for WPF controls is simply not predictable (let alone toolable, but we’ll get to that) so we end up needing a lot of sample code to get people off the ground. Plus, it’s really tough for developers.  You have a myriad (well, eight) different recommendations for doing the same thing and the choice between them is not always clear even to an experienced WPF developer.

That said, you may have thought that the complexity was worth it.  It may seem, after all, like this complex model puts flexibility in the designer’s hand since, of those eight, two (bindings and triggers) have a very strong markup component to them.  Moreover, the Silverlight way (the named parts approach) looks a strong code component to it. 

The thing that is easy to forget, though, is the fact that all of those bindable and triggerable properties and events had to be defined by a developer at one point.  The IsMouseOver property had to be created somewhere.  So at the end of the day, it’s not that behaviors require more code now, it’s just a different kind of code: more event handlers and fewer property change handlers to kick off behaviors.

Second, the Silverlight model has a very good chance of being much easier to use in a tool like Blend.  Frankly, I haven’t seen a lot of people have great success with control templating in Blend.  Button and Checkboxes don’t count.  Try creating a template for a ComboBox or a Menu.  It’s really hard, even if you know what you’re doing.  Once again, this comes down to the complexity of the contract between the template and code. 

Imagine, on the other hand, a model where you can simply walk through a checklist and provide elements for each of the UI "parts" of the template and storyboards for each of the "behaviors."  Menu might have a lot of parts and behaviors, but at least there is a predicable way to discover and provide them.  In this kind of a model, the designer could theoretically retain much (if not all) of the flexibility he has in WPF but have a much easier time accessing it.

Silverlight 2.0 isn’t there yet, but the model they’ve chosen is at least predictable and declarable and that gets us much closer to the templating Xanadu that I just described.  Even in the meantime, we still have a model that’s much easier to understand and even code for and the only trade off is that now I the developer exposes behaviors by responding to events instead of responding to property changes.

Now having said all that, I would love to have triggers in control templates, styles and especially data templates in Silverlight.  In fact, not having triggers in data templates has resulted in some serious hackery on my part (in which I end up creating a UserControl everytime we have a data template with any complexity).  But I like where this new templating model is going and I hope it goes even further. 

8 Comments

Jeremiah Morrill / MAR 25 2008

Wow the first positive remark I’ve read about SL control templating. I have not developed an opinion on it just yet, but I think Dr. WPF and yourself have conflicting perspectives on the subject.

So…Both as gentlemen of honor, I suggest you two have a duel. I believe it worked out well for Alexander Hamilton. ;)

-Jer

Marlon Grech / MAR 28 2008

I love you blog but Sorry, I cannot agree with you on this one. By choosing this model SL will break the Developer + Designer workflow. The designer will be too dependent on the Developer and the Developer will have to swear and change the controls just because the Designer decided to change something… lookless controls were a tried and tested SUCCESS. Why do we want to change that in SL and try to re invent something else!

notstatic.com / MAY 02 2008

[...] took some flack for my (arguably polemic) thoughts on the Silverlight control templating model so I was happy to see the second to last paragraph of this post where John shares some of my views [...]

Flatlander / JUN 11 2008

Why the Silverlight VisualStateManager is wrong…

Silverlight has a control templating system similar to that of Windows Presentation Foundation. However, Silverlight and WPF take different approaches to updating the user interface as the user interacts with it. WPF uses triggers: the creator of a tem…

nerdplusart.com | A Survey of Silverlight 3D / JUL 27 2008

[...] Okay, so you’d be a little right in thinking that but, come on, it’s only one and a half versions old.  Let’s cut Silverlight some slack and hope that we get some official support soon (but not before Triggers). [...]

Liquidboy / JUL 29 2008

here!here! Give Silverlight a break, it’s made some seriously great decisions in it’s control/parts model and VSM … I’ve been using Design / Blend & VS2008 religiously since they came out and I can honestly say that it works great for me.

I’m more a designer these days than a developer. Blend is evolving BUT completely agree with you that it’s not very easy to use when trying to template a datagrid (the hardest of all controls).

Triggers im not too strong on, if it’s sole purpose is for designers to be able to wire up the UI to the VSM so that they don’t need a developer then I’m definetely against that. As you’ve mentioned above one of the main reasons for triggers are a way to describe in XAML the connections between UI and VS (Visual State), this is a good justification for including triggers in Silverlight BUT what concerns me is that we may get to a state where people will code half there UI-VS connections in Triggers and the other half in code NOW THAT WILL BE A DANGEROUS THING!!

I think we need to really look at why people use triggers in WPF and under what conditions they choose code over triggers. Only when we really understand the benefits of triggers and once we have a good story to sell to developers then we should include it in Silverlight.

ps. the same thing can be said for people that wire up events in XAML versus those that wire them up in code. Which way is better ?? There is no perfect answer for this situation BUT i do know that there is alot of dirty/lazy coders out there that do both whenever they feel like it. Don’t let the same thing happen with triggers!!!!