<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>IanG on Tap</title><link>http://www.interact-sw.co.uk/iangblog/</link><description>Ian Griffiths in Weblog Form</description><copyright>(C) 2004 I D Griffiths</copyright><lastBuildDate>Sat, 05 Sep 2009 15:42:01 GMT</lastBuildDate><generator>Interact Blogger</generator><managingEditor>ian@interact-sw.co.uk</managingEditor><webMaster>ian@interact-sw.co.uk</webMaster><item><title>Silverlight 3 at PDC</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2009/09/05/silverlight-3-pdc</guid><link>http://www.interact-sw.co.uk/iangblog/2009/09/05/silverlight-3-pdc</link><pubDate>Sat, 05 Sep 2009 15:42:01 GMT</pubDate><description>&lt;p&gt;I’m absurdly excited about &lt;a href="http://microsoftpdc.com/"&gt;PDC&lt;/a&gt; this year because for the first time, I’ll be speaking there! &lt;a href="http://twitter.com/RichGee"&gt;Rich Griffin&lt;/a&gt; and I will be &lt;a href="http://microsoftpdc.com/Sessions/Getting-the-most-out-of-Silverlight-3"&gt;talking about Silverlight 3&lt;/a&gt;. This is one of the day-long &lt;a href="http://microsoftpdc.com/Workshops"&gt;workshops&lt;/a&gt; that run the day before the full conference starts.&lt;/p&gt;
&lt;p&gt;I’m pleased to be talking with someone who I’ve worked with as a fellow developer—Rich and I have collaborated on some of the many WPF and Silverlight projects we’ve worked on over the last few years. We both have a lot of experience solving real problems, and we’re looking forward to bringing that to the workshop—we will be talking about building real software.&lt;/p&gt;
&lt;p&gt;PDC is just over a couple of months away, so we haven’t quite set our talks in stone yet. We’d both love to &lt;a href="mailto:ian@interact-sw.co.uk;gripp@hotmail.com?subject=SL3%20PDC%20Workshop%20Suggestions"&gt;hear from you&lt;/a&gt; if you’re contemplating coming to our session and have ideas on what you’d like to see. That said, we obviously need to give people a fair idea of what to expect so they can plan now. So here’s what we’re currently thinking.&lt;/p&gt;
&lt;p&gt;We see the day breaking down into four pieces, taking roughly a quarter of the day each: The Ideas of Silverlight; Connecting; Software Design; Working with Designers. &lt;/p&gt;
&lt;h3&gt;The Ideas of Silverlight&lt;/h3&gt;
&lt;p&gt;I want to start by saying what this will &lt;i&gt;NOT&lt;/i&gt; be. We absolutely don’t want to do a laundry list of Silverlight features, or a straight forward walk through of the developer and the design tools. We believe that a sound understanding of a critical set of ideas at the heart of Silverlight is the secret to true proficiency.&lt;/p&gt;
&lt;p&gt;So we plan to talk about some features that make Silverlight very different from most other UI technologies, and how they combine in ways you might not expect. In particular, we plan to describe the roles played by templates, and styles, the structure of controls, the surprising importance of data binding, and why something called MVVM is so important. We also want to talk about some of the practical issues around deployment, testing, and tools.&lt;/p&gt;
&lt;h3&gt;Connecting&lt;/h3&gt;
&lt;p&gt;Silverlight is for the internet, so our second section will be all about how to connect with the rest of the world. We plan to look at WCF, REST, and POX in some depth. We’ll discuss the tension between security and flexibility, and some practical resolutions. We’ll talk about RIA services too, although since that’s a topic that could easily fill a day on its own, we don’t intend to cover it in depth—our current thinking is that we want to make it clear what it’s for and when you would or wouldn’t use it, illustrating just enough of it that you’ll see its overall approach. (Although if you think we should dedicate a large chunk of the day to RIA services, we’d like to hear from you, and we’d like to know what you think we should drop to make space for it.)&lt;/p&gt;
&lt;h3&gt;Software Design&lt;/h3&gt;
&lt;p&gt;It’s all very well to look at the core ideas, features and technologies of Silverlight, both on the inside, and where your code meets the rest of the world, but how do you tie it all together in practice? We’ll look at strategies for structuring your application’s design. (That’s software design, as opposed to interaction design or visual design, by the way.) We expect to go into more detail on MVVM here. We’d also like to talk about where technologies such as MEF and Prism can fit in.&lt;/p&gt;
&lt;h3&gt;Working with Designers&lt;/h3&gt;
&lt;p&gt;Yes, PDC is for developers. So is this session. Building Silverlight applications that look great takes more than throwing a finished application over the wall to where the visual designers work and asking them to slap a layer of sparkle on at the last minute. Integrating good visual design into your application requires planning. Developers need to understand the process and get involved. The way to get the best results is for developers and designers to work together closely. We’ll talk about the steps you need to take as developers to make this possible. We’ll also show how Blend fits into the world of the developer, and we’ll offer tips for how it can make your life easier when creating Silverlight applications.&lt;/p&gt;
&lt;p&gt;So that’s the current plan. I’ll be posting more as we refine our ideas, and most likely &lt;a href="http://twitter.com/idg10"&gt;tweeting&lt;/a&gt; &lt;a href="http://twitter.com/RichGee"&gt;too&lt;/a&gt;—we might do a couple of polls to gauge the level of interest in the various topics, so now is the perfect time for feedback. We look forward to your suggestions!&lt;/p&gt;
&lt;p&gt;(If you’re wondering about why I only just mentioned this, when it was all announced a few weeks ago, I came on board to speak just as I went off on an extended holiday, and now that I’m back, I’m feverishly trying to catch up with everything.)&lt;/p&gt;</description></item><item><title>Designing a WPF Timeline Control Part 1: Structure</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2009/06/04/wpf-timeline-control-design</guid><link>http://www.interact-sw.co.uk/iangblog/2009/06/04/wpf-timeline-control-design</link><pubDate>Thu, 04 Jun 2009 02:38:22 GMT</pubDate><description>&lt;p&gt;For a change, I’m involved in an endeavour I can blog about. I’m working with &lt;a href="http://mcwtech.com/CS/blogs/brianr/default.aspx"&gt;Brian Randell&lt;/a&gt;, &lt;a href="http://blogs.ythos.net/"&gt;Matthew Adams&lt;/a&gt;, and &lt;a href="http://blogs.conchango.com/felixcorke/"&gt;Felix Corke&lt;/a&gt; on a &lt;a href="http://mcwtech.com/CS/blogs/brianr/archive/2009/03/29/dinner-and-a-new-project.aspx"&gt;project&lt;/a&gt; that involves building a kind of timeline control. This blog describes the thinking behind the control’s structure—WPF controls tend to end up looking very different from their equivalents in anything based on classic Win32 UI underpinnings such as Windows Forms, and I thought it might be interesting to explain how and why the differences come about.&lt;/p&gt;
&lt;p&gt;We’re not quite ready to show the code for this yet, as it’s still a work in progress, but we’ll make the source available before too long.&lt;/p&gt;
&lt;h3&gt;Requirements&lt;/h3&gt;
&lt;p&gt;The requirements were a little nebulous, because we set out to build a control for fun, rather than for any particular application or customers. So one of the most important requirements was that it be interesting to build. Nonetheless, we had some ideas about what sorts of things we wanted to be able to do. For example we wanted a way to show files found via &lt;a href="http://www.microsoft.com/windows/products/winfamily/desktopsearch/default.mspx"&gt;Windows Search&lt;/a&gt; (or, as I find it hard to stop calling it, Windows Desktop Search). Positioning and grouping files by dates along a timeline seemed like it might be an interesting way to visualize search results.&lt;/p&gt;
&lt;p&gt;Here’s an early prototype we built to get a rough idea of how it might work with some real data. I’m jumping ahead here a little, but a picture makes it easier to understand what we had in mind:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.interact-sw.co.uk/images/Crummy-Timeline.png" alt="Screenshot of prototype timeline user interface - this is what happens when you let developers design UX"&gt;&lt;/p&gt;
&lt;p&gt;The user can drag the whole timeline left or right to move around, and zoom in and out with the mouse. The items are all expanded in that example above. It doesn’t have to be that way though—here’s how it might look when the user has zoomed out a bit and only has one item expanded:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.interact-sw.co.uk/images/Crummy-Timeline-Distribution.png" alt="Prototype timeline showing mostly collapsed groups, with a sort of bar graph representation of how much stuff is in these groups"&gt;&lt;/p&gt;
&lt;p&gt;Clearly we have a bit of a problem with the captions on the timeline here, but you can see how this gives an impression of which bits of the timeline have a lot of content, and which bits are empty. Notice that in this example we also experimented with vertically stacked bars to indicate how much stuff was inside each group, to give a better idea of information density over time.&lt;/p&gt;
&lt;p&gt;Now obviously this example looks horrible. Our goal at this stage was to try out some ideas to see if they would work before building something properly. As you’ll see in future blogs, it started looking a lot better once Felix got his hands on it.&lt;/p&gt;
&lt;p&gt;Another feature we wanted was the ability to support multiple levels of detail—we envisaged the initial view as being a bunch of blobs on the timeline indicating files placed by date, which could be expanded to reveal details, possibly multiple levels of detail. So the examples above aggregate a bunch of files that appear in quick succession as a single blob, showing a list of all the files if you click on that blob, but we might want to go further, with some sort of details fly-out available on each of the items in that list.&lt;/p&gt;
&lt;p&gt;We also wanted to avoid assuming much about the nature of the data source. We had discussed various ideas for what we might want to display along a timeline—I forget the complete list, but TV schedule information, appointment booking, gaming, and project management came up as possible application areas.&lt;/p&gt;
&lt;p&gt;So our ‘spec’ was pretty vague (although I’ve had worse). We decided to start with the file system/search example, and see where it took us—more a voyage of exploration than a software project. Perhaps not the most efficient approach to software development, but if you’re building a UI component of a kind you’ve not attempted before, it’s not a bad idea to be at least a little chaotic—your first design is unlikely to be your best, so some space to experiment is useful.&lt;/p&gt;
&lt;p&gt;But where to start? If you want a custom control to get the best out of WPF, it’s a good idea to try and align yourself with WPF’s philosophy. But what is that, exactly?&lt;/p&gt;
&lt;h3&gt;Composition is King&lt;/h3&gt;
&lt;p&gt;I’ve long thought that WPF’s single biggest benefit is its composition-based approach. Examine most of the built-in controls, and you’ll find that they do less work than you might think, deferring to other WPF components to get the job done. And these subordinate components are almost invariably multi-purpose—they show up in other WPF controls, and can be used where appropriate in your own custom controls.&lt;/p&gt;
&lt;p&gt;Take the &lt;code&gt;ListBox&lt;/code&gt;. Amongst the things it doesn’t know how to do are: size and position the list items; render list items; provide selection highlighting; provide its own appearance. For these tasks it defers to its items panel, its item template, its item containers, and its control template respectively, and you can plug in your own objects for any of these, to customize the control. Of these, only the item container type (&lt;code&gt;ListBoxItem&lt;/code&gt;) is specific to the &lt;code&gt;ListBox&lt;/code&gt;, and even that shares a lot in common with other list controls’ items containers.&lt;/p&gt;
&lt;p&gt;A single monolithic black box of a control might be in keeping with long-standing traditions of the Windows 3rd party control market, but it’s not the WPF way.&lt;/p&gt;
&lt;h3&gt;Areas of Responsibility&lt;/h3&gt;
&lt;p&gt;The WPF way is for each area of responsibility to be owned by a separate type. This is not a new idea, of course, but it’s one that seems to be less well represented in the UI layer than elsewhere.&lt;/p&gt;
&lt;p&gt;When developing new UI element types, we design them to be combined in the ways we need them to work, and with luck, it will be possible to combine them usefully in ways we did not first anticipate. In fact that second part often requires more than luck—types are rarely reusable in practice until you’ve tried to use them in multiple scenarios and have fixed the problems that made them less useful than you had hoped. We haven’t done that yet for these controls, so this is probably not yet the definitive design.&lt;/p&gt;
&lt;p&gt;So, what are the areas of responsibility we think exist for our timeline UI? Here’s a breakdown, along with a suggestion of what kind of object might fit the bill:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Generation of UI elements from a data source (&lt;code&gt;ItemsControl&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Visual representation of the ‘line’ in the timeline control (&lt;code&gt;ControlTemplate&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Control to provide panning/scrolling (&lt;code&gt;ScrollViewer&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Positioning of UI elements along a line (&lt;code&gt;Panel&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Generated element type to be positioned on the line (some sort of &lt;code&gt;ContentControl&lt;/code&gt; or &lt;code&gt;Expander&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Visual representation of an item on the line (&lt;code&gt;ControlTemplate&lt;/code&gt;?)&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Expanded view of content of generated element (&lt;code&gt;DataTemplate&lt;/code&gt;?)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ll notice that we have borrowed shamelessly from the pattern established by WPF’s various items controls. We can produce a very similar-looking list for &lt;code&gt;ListBox&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Derives from &lt;code&gt;ItemsControl&lt;/code&gt;, enabling it to generate items from a data source&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Being a control it has a &lt;code&gt;ControlTemplate&lt;/code&gt; to define its appearance&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Relies on &lt;code&gt;ScrollViewer&lt;/code&gt; to provide a scrollable area&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Uses a &lt;code&gt;VirtualizingStackPanel&lt;/code&gt; to position its elements vertically&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;Generates a &lt;code&gt;ListBoxItem&lt;/code&gt; (which is a &lt;code&gt;ContentControl&lt;/code&gt;) for each item in the list&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;&lt;code&gt;ListBoxItem&lt;/code&gt; is a control, so it also has a template&lt;/li&gt;
&lt;li&gt;
    &lt;p&gt;&lt;/p&gt;&lt;code&gt;ItemTemplate&lt;/code&gt; or implicit data template used to render items in the list&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If WPF already has a pattern that meets your needs, it’s usually a good idea to follow suit. Anyone already familiar with WPF will find your code easier to follow, and it also means you’re on a path likely to lead somewhere useful. But the degree of similarity here raises an obvious question: could we get away without creating any custom elements at all?&lt;/p&gt;
&lt;h3&gt;No Custom Elements?&lt;/h3&gt;
&lt;p&gt;It’s surprisingly common for WPF applications to be able to cobble together various WPF elements to achieve effects that would have required a custom control in Windows Forms. Could we do that here?&lt;/p&gt;
&lt;p&gt;As an early experiment we built an application that queried Windows Search for all the bitmaps on a system, used LINQ to group them by month, and dumped the results into an &lt;code&gt;ItemsControl&lt;/code&gt; to produce a timeline. To keep it simple we just used a &lt;code&gt;StackPanel&lt;/code&gt; to get the items in order, but not yet positioned precisely according to date. The &lt;code&gt;ItemTemplate&lt;/code&gt; contained a list of the files, using an &lt;code&gt;Image&lt;/code&gt; element to render the actual image file. This list was initially collapsed, but could be expanded by clicking a button.&lt;/p&gt;
&lt;p&gt;It rapidly became obvious that this wasn’t going to fly from a performance perspective. Even with the image lists initially collapsed, mere invisibility wasn’t enough to prevent the &lt;code&gt;Image&lt;/code&gt; elements from being created. This meant that when the application started, it would attempt to load every single bitmap found by Windows Search. We let it run for a while to enjoy the spectacle of a single process consuming over 4GB of memory—it’s entertaining to pretend you have a justification for owning a 64-bit system—before giving up and killing it off.&lt;/p&gt;
&lt;p&gt;The naive approach clearly wasn’t going to work, but we could make a small change to reduce the memory footprint: we decided to take the image loading out of WPF’s &lt;code&gt;Image&lt;/code&gt; element’s hands. Instead of binding the &lt;code&gt;Image&lt;/code&gt; element’s &lt;code&gt;Source&lt;/code&gt; directly to the bitmap path, we wrote some code to load the image. This gave us the option to specify a size for the loaded image—since we were only showing thumbnails, we told WPF to decode the image to a much smaller size, just 40x30 pixels. This way it was at least conceivable that all the images we were trying to load might fit in memory. This ‘improved’ matters in the sense that the application progressed from not working at all to working unusably slowly.&lt;/p&gt;
&lt;p&gt;It was starting to look like some form of virtualization might be necessary.&lt;/p&gt;
&lt;p&gt;(This is of course all utterly predictable. Less subtle minds than your own might even conclude that we were idiots for bothering to try this naive approach. But I’ve learned that it’s wise to start your performance investigations with the parts you can predict. That way you get to find out which of your predictions were wrong. Plus, you might get to observe things going wrong in a slightly different fashion from the one you expected, which can offer useful insights. Indeed, besides the rather obvious expected memory consumption problems, we observed some CPU-bound performance issues in the image handling in some places that surprised us, and which uncovered an opportunity to significantly speed up our eventual load time performance which might have been harder to spot at a later stage. I’ll cover this in a later blog.)&lt;/p&gt;
&lt;p&gt;A couple of options stood out for improving the bitmap handling. We could get slightly cleverer with the expansion of the per-group list: rather than merely collapsing the list until the user expanded it, we could arrange not to create the list at all until needed—that should avoid the premature (and rather expensive) loading of every bitmap on the system. Or we could use a virtualizing panel for the main timeline items control, so that the groups’ UIs didn’t get instantiated at all until they appeared on screen. We tried both.&lt;/p&gt;
&lt;p&gt;Either of these on their own produced a profound improvement—the application went from “Fail slowly” to “Sub-second load” performance. However, using just one or the other wasn’t enough to get that “sub-second” quite “sub-” enough—we were seeing a half second UI freeze between the Windows Search results coming back and the results being displayed. Only with both techniques applied was the performance satisfactory.&lt;/p&gt;
&lt;p&gt;So we need at least one custom element: our panel needs to virtualize, and the only built-in virtualizing panel is the &lt;code&gt;VirtualizingStackPanel&lt;/code&gt;. That doesn’t give us the positioning we want: we need to be able to select a position based on some date. Only &lt;code&gt;Canvas&lt;/code&gt; and &lt;code&gt;Grid&lt;/code&gt; make that sort of control possible, and neither of those can virtualize. So we need our own panel. We provisionally chose to call this a &lt;code&gt;VirtualizingOffsetPanel&lt;/code&gt;, for want of a better name.&lt;/p&gt;
&lt;p&gt;We also decided to build a custom control to perform the expansion. It is technically possible to get the deferred list realization needed with an &lt;code&gt;Expander&lt;/code&gt;: the trick is to use a &lt;code&gt;ContentTemplate&lt;/code&gt; rather than just making our image list the child of the &lt;code&gt;Expander&lt;/code&gt;. However, we’d be relying on a side effect of an undocumented &lt;code&gt;Expander&lt;/code&gt; behaviour. I’ve gone down a similar path before with tree view item expansion, and didn’t enjoy it. So we decided to make our own expander (which is a pretty simple thing to construct) in order to give us control over what actions occur and in which order when an item is expanded. And since this control was going to be instantiated for each item on the timeline, we decided to designate this as the item container for our timeline control. And since a custom item container requires a custom items control, we have a reason for a third custom element, the &lt;code&gt;TimelineControl&lt;/code&gt; itself.&lt;/p&gt;
&lt;p&gt;So it’s now looking like we’re going to write three custom elements: a &lt;code&gt;TimelineControl&lt;/code&gt;, a &lt;code&gt;TimelineGroupItem&lt;/code&gt;, and a &lt;code&gt;VirtualizingOffsetPanel&lt;/code&gt;. Yes, for that second one, the name &lt;code&gt;TimelineControlItem&lt;/code&gt; might have looked more consistent, but &lt;code&gt;TimelineGroupItem&lt;/code&gt; felt like a more accurate description of what it represented. So far, it still feels that way, so we’re still calling it that.&lt;/p&gt;
&lt;h3&gt;Aside: To Group or Not to Group&lt;/h3&gt;
&lt;p&gt;If you’re familiar with the capabilities of WPF data binding, you might be thinking: hold on, why aren’t you using &lt;code&gt;CollectionView.GroupDescriptions&lt;/code&gt; and a &lt;code&gt;GroupStyle&lt;/code&gt;? Well we tried that, but it seems that grouping in &lt;code&gt;ItemsControls&lt;/code&gt; has the side-effect of disabling virtualization. And since we need virtualization to get acceptable load performance, that’s a non-starter.&lt;/p&gt;
&lt;p&gt;In any case, this didn’t seem like a great loss. After a fiddling around with Windows Search for a while, LINQ started to look like an easier and more flexible way to manage grouping.&lt;/p&gt;
&lt;h3&gt;Prototyping Matters&lt;/h3&gt;
&lt;p&gt;Trying to build a working example with no custom elements at all was an important exercise. We had outlined a rough design of what we thought we probably wanted before we started. This up-front design was by no means ‘big’, but we got something wrong nonetheless. We originally thought we’d want not just the timeline and group item controls, but also an individual timeline item control. But in working through the prototype, no clear need for such a control emerged—it seems that having established a need for the &lt;code&gt;TimelineControl&lt;/code&gt;, &lt;code&gt;TimelineGroupItem&lt;/code&gt;, and &lt;code&gt;VirtualizingOffsetPanel&lt;/code&gt; items, a combination of content controls and data templates was enough to finish off the job.&lt;/p&gt;
&lt;p&gt;In future blog entries I’ll talk about how we went about building each of these custom elements.&lt;/p&gt;</description></item><item><title>Oslo WPF and Silverlight Demos</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2009/04/30/oslo-wpf-silverlight</guid><link>http://www.interact-sw.co.uk/iangblog/2009/04/30/oslo-wpf-silverlight</link><pubDate>Thu, 30 Apr 2009 15:49:57 GMT</pubDate><description>&lt;p&gt;This week I've been in Oslo, teaching a combined &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP19"&gt;Silverlight&lt;/a&gt; and &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP15"&gt;WPF&lt;/a&gt; course. For those of you who attended, here are the  &lt;a href="http://www.interact-sw.co.uk/downloads/WpfSilverlightDemos2009-04.zip"&gt;demos for download&lt;/a&gt;. And for those of you who didn't attend, well, you can download the demos too if you like.
&lt;/p&gt;</description></item><item><title>Silverlight and WriteableBitmap</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/11/07/silverlight-writeable-bitmap</guid><link>http://www.interact-sw.co.uk/iangblog/2008/11/07/silverlight-writeable-bitmap</link><pubDate>Fri, 07 Nov 2008 12:19:55 GMT</pubDate><description>&lt;p&gt;I’ve just released a library to CodePlex that implements a feature missing from &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP19"&gt;Silverlight&lt;/a&gt;: the ability to generate bitmaps from pixels in code at runtime. WPF provides this through its &lt;code&gt;WritableBitmap&lt;/code&gt; class, but Silverlight 2 doesn’t have that. My &lt;a href="http://www.codeplex.com/SlDynamicBitmap"&gt;SlDynamicBitmap&lt;/a&gt; project offers a solution.&lt;/p&gt;
&lt;p&gt;I was inspired to create this by the recent reports of the &lt;a href="http://adamkinney.com/blog/374/default.aspx"&gt;Silverlight port of Quake&lt;/a&gt;. From what the author says, it’s clear that they must have devised some sort of mechanism for pushing raw pixels onto the screen, something Silverlight really doesn’t help you with. I’ve been toying for a while with an idea about how you might solve this, but hadn’t tried it before. Quakelight shows that it’s definitely possible, which encouraged me to try out my idea.&lt;/p&gt;
&lt;p&gt;Incidentally, I have no idea how Quakelight solves this problem. It may well have a much more elegant solution than the ghastly hack I’m perpetrating here.&lt;/p&gt;
&lt;h3&gt;Why Would I Want This?&lt;/h3&gt;
&lt;p&gt;Before I show how it works, it’s worth reviewing why you would even want a Silverlight equivalent of WPF’s WriteableBitmap – what’s it for? It’s useful for when you want to generate pixels at runtime based on some kind of algorithm. Ray tracing is one classic example – the computer constructs an image one pixel at a time, and in order to display the results, you need some way of controlling every single pixel on a region of the screen, rather than using higher level primitives.&lt;/p&gt;
&lt;p&gt;To illustrate this I’ve chosen another classic example: a &lt;a href="http://www.interact-sw.co.uk/slapps/mandelbrot/"&gt;Mandelbrot set fractal renderer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.interact-sw.co.uk/images/Silverlight-Mandelbrot.png" alt="Silverlight-Mandelbrot.png"&gt;&lt;/p&gt;
&lt;p&gt;In case you’re not familiar with &lt;a href="http://en.wikipedia.org/wiki/Mandelbrot_set"&gt;the Mandelbrot set&lt;/a&gt;, it’s a fractal generated with a deceptively simple process. It’s a striking demonstration of how incredibly basic non-linear systems can exhibit behaviour that is very complex, and which shows some very surprising geometric scaling features.&lt;/p&gt;
&lt;p&gt;But for my present purpose, the interesting thing about the Mandelbrot set is that you end up with a program that generates a lot of pixels and needs some way to get them on the screen. In WPF, this is the kind of scenario that &lt;code&gt;WriteableBitmap&lt;/code&gt; is for. To enable this in Silverlight, I’ve written a new class called &lt;code&gt;PngGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;It’s Called What?&lt;/h3&gt;
&lt;p&gt;I’ve chosen to call the main type in this library &lt;code&gt;PngGenerator&lt;/code&gt;, and not &lt;code&gt;WriteableBitmap&lt;/code&gt;. This may seem like an odd choice, but there are two good reasons for it. First, I regard this code as a workaround for a missing feature, not a proper solution, so I’m hoping that Silverlight will eventually get a &lt;code&gt;WriteableBitmap&lt;/code&gt; of its own. So it would have been unhelpful of me to use the same name. Second, the usage model has ended up being pretty different – it’s not really possible to write a custom class in Silverlight that’s a drop-in replacement for WPF’s &lt;code&gt;WriteableBitmap&lt;/code&gt;. Only Microsoft can provide us with that – it would require support from the Silverlight plug-in itself to build an exact replica.&lt;/p&gt;
&lt;p&gt;So that’s why it’s not called &lt;code&gt;WriteableBitmap&lt;/code&gt;. Why &lt;code&gt;PngGenerator&lt;/code&gt;? That’s because of the ghastly hack I used to make this work.&lt;/p&gt;
&lt;h3&gt;The Ghastly Hack&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;PngGenerator&lt;/code&gt; works by converting an array of pixel colour values into a &lt;code&gt;Stream&lt;/code&gt; of bytes that represent the image as a PNG file. (I chose PNG rather than JPEG because JPEG files typically use lossy compression, so you don’t get precisely the pixels you asked for. It has since been pointed out to me that JPEG does now support lossless images too, so I guess you could use either format. BMP would have been a whole lot easier as it’s a simpler format than either, but Silverlight only supports PNG and JPEG.)&lt;/p&gt;
&lt;p&gt;If Silverlight had an equivalent of WPF’s &lt;code&gt;PngBitmapEncoder&lt;/code&gt; class, this would have been pretty straightforward to write. Unfortunately Silverlight doesn’t contain any code to generate PNGs from pixels. So most of the code in this library is a PNG builder.&lt;/p&gt;
&lt;p&gt;By the way, I would &lt;b&gt;not&lt;/b&gt; recommend using this code to generate PNG files for general use. It does a couple of rather crufty things. It only generates the bare minimum contents required to be just enough of a legal PNG file for Silverlight to render it. And it also doesn’t compress the data. The ‘deflate’ compression format mandated by PNG lets you put in so-called ‘non-compressible’ blocks. You’re supposed to use this for any data that gets bigger when you ‘deflate’ it. Some data is just hard to compress, and the idea is that you mitigate this problem by storing that data verbatim. But I’m storing &lt;i&gt;all&lt;/i&gt; the pixel data this way to provide the simplest possible path from pixel to screen.&lt;/p&gt;
&lt;p&gt;So it’s a little too specialized to be a useful general-purpose PNG generator.&lt;/p&gt;
&lt;p&gt;I’ve seen nastier ways of solving this by the way. I’ve seen people generate Silverlight content made up of hundreds of tiny rectangles in an attempt to render their own pixel data! This doesn’t work all that brilliantly – you get anti-aliasing artefacts, and it’s mind-bogglingly slow. Unpleasant though the technique I’m using is, it does at least produce good quality results, and is tolerably quick, although not as good as a native &lt;code&gt;WriteableBitmap&lt;/code&gt; built into Silverlight could be.&lt;/p&gt;
&lt;h3&gt;Using the PngGenerator&lt;/h3&gt;
&lt;p&gt;To use the &lt;code&gt;PngGenerator&lt;/code&gt;, you do this sort of thing:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:dimgray"&gt;PngGenerator&lt;/span&gt; pngGen = &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:dimgray"&gt;PngGenerator&lt;/span&gt;(640, 480);
&lt;span style="color:dimgray"&gt;Color&lt;/span&gt;[] m_pixelData = GenerateMyPixels();
pngGen.SetPixelColorData(m_pixelData);
&lt;span style="color:dimgray"&gt;BitmapImage&lt;/span&gt; imgSource = &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:dimgray"&gt;BitmapImage&lt;/span&gt;();
imgSource.SetSource(pngGen.CreateStream());
myImageElement.Source = imgSource;
&lt;/pre&gt;
&lt;p&gt;That may seem slightly less direct than necessary. I’m contemplating adding a &lt;code&gt;CreateSource&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;)&lt;/code&gt; method that builds the &lt;code&gt;BitmapImage&lt;/code&gt; for you, to make the code a couple of lines shorter. But the verbose approach required by the current version does have the benefit of showing the process relatively clearly.&lt;/p&gt;
&lt;p&gt;Walking through it, we build a new &lt;code&gt;PngGenerator&lt;/code&gt;, telling it the image dimensions we require – 640x480 in this case. Then we build an array of &lt;code&gt;Color&lt;/code&gt; values indicating the pixel colours we want. (So &lt;code&gt;GenerateMyPixels&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;)&lt;/code&gt; here stands for whatever code you’ve got that generates pixel values.) We pass these pixels to the &lt;code&gt;PngGenerator&lt;/code&gt;’s &lt;code&gt;SetPixelColorData&lt;/code&gt; method. Then we construct an instance of Silverlight’s &lt;code&gt;BitmapImage&lt;/code&gt; type, and call its &lt;code&gt;SetSource&lt;/code&gt; method. (BTW, &lt;code&gt;BitmapImage.SetSource&lt;/code&gt; is the Silverlight feature that makes this possible. It accepts any &lt;code&gt;S&lt;/code&gt;&lt;code&gt;ystem.IO.S&lt;/code&gt;&lt;code&gt;tream&lt;/code&gt; object, and expects it to contain either a PNG or a JPEG.) The &lt;code&gt;PngGenerator&lt;/code&gt;’s &lt;code&gt;CreateStream&lt;/code&gt; method returns a &lt;code&gt;Stream&lt;/code&gt; that contains a PNG representing the pixels passed in to &lt;code&gt;SetPixelColorData&lt;/code&gt;. Finally, we use the &lt;code&gt;BitmapImage&lt;/code&gt; as the &lt;code&gt;Source&lt;/code&gt; property of a Silverlight &lt;code&gt;Image&lt;/code&gt; element, and the image appears.&lt;/p&gt;
&lt;p&gt;If you want to change the image, you just call &lt;code&gt;SetPixelColorData&lt;/code&gt; again, and then repeat the steps to build a &lt;code&gt;Stream&lt;/code&gt; and a &lt;code&gt;BitmapImage&lt;/code&gt;. Silverlight’s &lt;code&gt;BitmapImage&lt;/code&gt; only loads the PNG once – it doesn’t expect it to change – so we need to build a new one each time round. However, you’re free to pass the same array into &lt;code&gt;SetPixelColorData&lt;/code&gt; every time around, and internally, the &lt;code&gt;PngGenerator&lt;/code&gt; reuses all the same buffers for building the stream, so the costs of building a new frame aren’t as bad as they might be.&lt;/p&gt;
&lt;p&gt;(My first prototype did build everything from scratch each time round. It was about three times slower than the current implementation! Memory allocation and copying start to look expensive when you’re trying to push millions of pixels around several times a second.)&lt;/p&gt;
&lt;h3&gt;Silverlight and Multicore&lt;/h3&gt;
&lt;p&gt;Incidentally, the &lt;a href="http://www.interact-sw.co.uk/slapps/mandelbrot/"&gt;example&lt;/a&gt; also happens to be a clear demonstration of Silverlight’s ability to exploit multiple CPU cores on the client side. In fact the time I first ran into the absence of &lt;code&gt;WriteableBitmap&lt;/code&gt; in Silverlight was when I sat down to write exactly this Mandelbrot example as an illustration of how to use Silverlight’s multi-threading capabilities. At the time I had to abandon the idea due to the lack of writeable bitmap support. But finally, 6 months down the line, I can use the demo!&lt;/p&gt;
&lt;p&gt;The code that performs the calculations to generate the fractal image uses the thread pool to parallelize the work. Crude, but effective. On my quad core desktop, it’s really quick, despite the lack of any clever optimizations that some fractal generators use. And since I first started writing Mandelbrot rendering code on 8-bit micros with 2MHz processors back in the mid 1980s, which used to take about 3 hours to produce a puny 256x256 image, I can hardly believe how fast this runs on new hardware.&lt;/p&gt;
&lt;h3&gt;Missing Features&lt;/h3&gt;
&lt;p&gt;The code up on CodePlex is designated version 0.1 because it’s incomplete. It doesn’t support an alpha channel, i.e. no semi-transparent bitmaps. There’s no fundamental reason for this, it’s just that I’ve only had time to spend a couple of evenings on this so far. I plan to add this. I also want to add a mechanism for using raw byte arrays for pixel data instead of having to use &lt;code&gt;Color&lt;/code&gt; – there are some performance issues that this will solve. And there are a couple of places where the code is not as efficient as it could be. Right now on my machine, if I generate 1024x768 bitmaps as fast as possible it only manages about 15-20 frames per second, and I believe I can improve on that. (Although the tests I’ve performed suggests this is never going to do better than 30fps for 1024x768 on current hardware unless Silverlight evolves to provide native support for this feature.)&lt;/p&gt;
&lt;h3&gt;Getting Started&lt;/h3&gt;
&lt;p&gt;The code is up on the &lt;a href="http://www.codeplex.com/SlDynamicBitmap"&gt;SlDynamicBitmap&lt;/a&gt; project on CodePlex. You can &lt;a href="http://www.codeplex.com/SlDynamicBitmap/Release/ProjectReleases.aspx?ReleaseId=19105"&gt;download a ZIP containing both binary and source&lt;/a&gt;, or you can &lt;a href="http://www.codeplex.com/SlDynamicBitmap/SourceControl/ListDownloadableCommits.aspx"&gt;browse the source online&lt;/a&gt;. Enjoy!&lt;/p&gt;
&lt;p&gt;[&lt;b&gt;Update, 2008-11-07:&lt;/b&gt; &lt;a href="http://www.irritatedvowel.com/"&gt;Pete Brown&lt;/a&gt; pointed out to me that this has already been done... Joe Stegman has &lt;a href="http://blogs.msdn.com/jstegman/archive/2008/10/30/updated-source-for-image-samples.aspx"&gt;some code&lt;/a&gt; that enables
similar stuff. However, as far as I can tell my library is faster. Modifying Joe's code to do a 1024x768 image, it doesn't seem to be able to do more than about 5 frames per second. That's the same speed I got with my code before I started reducing the amount of copying and memory allocation. So while my contribution isn't original, its a few times faster than the existing work. So I hope it's still useful.]
&lt;/p&gt;</description></item><item><title>Oslo WPF and Silverlight Demos</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/10/17/oslo-wpf-silverlight</guid><link>http://www.interact-sw.co.uk/iangblog/2008/10/17/oslo-wpf-silverlight</link><pubDate>Fri, 17 Oct 2008 15:07:43 GMT</pubDate><description>&lt;p&gt;For those of you who saw me in Oslo this week, here are the &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP19"&gt;Silverlight&lt;/a&gt; and &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP15"&gt;WPF&lt;/a&gt; &lt;a href="http://www.interact-sw.co.uk/downloads/SilverlightWpfDemosOsloOct08.zip"&gt;demos for download&lt;/a&gt;.
&lt;/p&gt;</description></item><item><title>WPF Course in London</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/09/15/wpf-course-london</guid><link>http://www.interact-sw.co.uk/iangblog/2008/09/15/wpf-course-london</link><pubDate>Mon, 15 Sep 2008 16:57:54 GMT</pubDate><description>&lt;p&gt;Shameless marketing plug alert!&lt;/p&gt;
&lt;p&gt;I’ll be teaching the &lt;a href="http://www.pluralsight.com/"&gt;Pluralsight&lt;/a&gt; &lt;a href="http://www.pluralsight.com/main/ilt/course.aspx?id=AP15"&gt;WPF Fundamentals course&lt;/a&gt; in London later this month. (It runs from 29th September to 2nd October, so I guess that’s later this month and also early next month.) The course is for software developers, and offers detailed information on how WPF works, and how to use it effectively.&lt;/p&gt;
&lt;h3&gt;A Year of Online Training Included&lt;/h3&gt;
&lt;p&gt;If you attend, you’ll also get access to an online version for a year – this includes recordings of all the talk content and demos. While Pluralsight’s &lt;a href="http://www.pluralsight.com/main/olt/courses.aspx"&gt;Online Training&lt;/a&gt; service is still in beta, this is one of the “select Pluralsight instructor-led courses” that will get you access to the online WPF material today without having to be on our private beta program.&lt;/p&gt;
&lt;p&gt;This is why you’ll be asked to sign up for an account if you &lt;a href="https://www.pluralsight.com/main/ilt/registration/default.aspx?c=320"&gt;register for the WPF training online&lt;/a&gt; – I dislike it when sites make you create an account just to buy something, but there is a good reason in this case: the login gets you to the online content that’s included as part of the course. And we think the online material makes a great addition – there’s a lot of content crammed into the course, so we hope you’ll find it useful to be able to go back and review it at your leisure.&lt;/p&gt;</description></item><item><title>LINQ Aggregation and Bounding Boxes</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/07/30/linq-box-aggregation</guid><link>http://www.interact-sw.co.uk/iangblog/2008/07/30/linq-box-aggregation</link><pubDate>Wed, 30 Jul 2008 18:56:27 GMT</pubDate><description>&lt;p&gt;I recently worked on a project that included code to calculate the bounding box of a collection of rectangles. The code did more or less what you might expect – it iterated over the collection, and any time a new rectangle wasn’t contained by the current bounds, it enlarged the bounds to fit.&lt;/p&gt;
&lt;p&gt;There was nothing wrong with this, but I knew of a cute trick that could do the job with a fraction of the code. I wasn’t being paid to replace working code with cute tricks, so I left it as it was – when it comes to production code I’m not a fan of the “If it ain’t broke, let me take a crack at it” school of coding. So I thought I’d illustrate the idea here instead.&lt;/p&gt;
&lt;p&gt;Here’s a one-line implementation of the same logic, using the &lt;code&gt;System.Windows.Rect&lt;/code&gt; type in WPF. (Note that this is a very simple value type, not a full UI element. It’s just 4 numbers.)&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt; GetBounds(&lt;span style="color:dimgray"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;&amp;gt; rects)
{
    &lt;span style="color:blue"&gt;return&lt;/span&gt; rects.Aggregate((currBounds, nextRect) =&amp;gt; &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(currBounds, nextRect));
}
&lt;/pre&gt;
&lt;p&gt;That’s pleasingly succinct, although possibly a little obscure if you’re not accustomed to this style of coding. Actually we can go one more notch on the obscure-and-succinct scale. This comes to the same thing:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt; GetBounds(&lt;span style="color:dimgray"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;&amp;gt; rects)
{
    &lt;span style="color:blue"&gt;return&lt;/span&gt; rects.Aggregate(&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union);
}
&lt;/pre&gt;
&lt;p&gt;LINQ’s standard Aggregate method implements a mechanism often known as &lt;a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)"&gt;fold&lt;/a&gt; in functional programming languages. It’s a pretty simple idea: apply some sort of cumulative operation across all items in a list to build up some sort running total. In this example, LINQ’s uses the first item in the list as the starting point. (Alternatively, you can provide an explicit start value.) It passes that in to the function supplied, along with the second item. The result becomes the new running total, which is fed again into the function supplied, this time along with the third item...and so on. Each time round the aggregating function we provide gets the running total as its first argument, and the next item in the list as its second.&lt;/p&gt;
&lt;p&gt;It’s easier to show what it does than to describe it. Here’s what that code would do if applied to an array containing five Rects:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(rs[0], rs[1]), rs[2]), rs[3]), rs[4])
&lt;/pre&gt;
&lt;p&gt;Or if you prefer sequential imperative code, you might like to picture it as:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:dimgray"&gt;Rect&lt;/span&gt; currBounds = rs[0];
currBounds = &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(currBounds, rs[1]);
currBounds = &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(currBounds, rs[2]);
currBounds = &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(currBounds, rs[3]);
currBounds = &lt;span style="color:dimgray"&gt;Rect&lt;/span&gt;.Union(currBounds, rs[4]);
&lt;/pre&gt;
&lt;p&gt;As you can see, it’s not particularly magical. It’s nothing you couldn’t implement with a straightforward loop. (Or with tedious duplication, as here.) But it’s an idea that crops up often enough that it’s rather handy to have as general purpose function – you get to specify what you’d like to use as the cumulative operation, and LINQ does the rest for you.&lt;/p&gt;
&lt;p&gt;Aggregate can be used to implement some common operations. For example, you can use it to sum together all the numbers in a list:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; Sum(&lt;span style="color:dimgray"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
    &lt;span style="color:blue"&gt;return&lt;/span&gt; numbers.Aggregate((currTotal, nextInt) =&amp;gt; currTotal + nextInt);
}
&lt;/pre&gt;
&lt;p&gt;Or you could use it to find the highest number in a list:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; Max(&lt;span style="color:dimgray"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
    &lt;span style="color:blue"&gt;return&lt;/span&gt; numbers.Aggregate((highest, nextInt) =&amp;gt; &lt;span style="color:dimgray"&gt;Math&lt;/span&gt;.Max(highest, nextInt));
}
&lt;/pre&gt;
&lt;p&gt;or again, since this just forwards the arguments directly to a function, we can skip the lambda and use the more terse form:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:blue"&gt;int&lt;/span&gt; Max(&lt;span style="color:dimgray"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
    &lt;span style="color:blue"&gt;return&lt;/span&gt; numbers.Aggregate(&lt;span style="color:dimgray"&gt;Math&lt;/span&gt;.Max);
}
&lt;/pre&gt;
&lt;p&gt;Of course, you don’t need to write these two examples, because LINQ already supplies Sum and Max extension methods for you. But conceptually at least, they’re just a handy shortcut to a specialized form of aggregation.&lt;/p&gt;
&lt;p&gt;Getting the total bounding box of all of the rectangles in a list is not so different from finding the highest number – it’s just a slightly different kind of maximum.&lt;/p&gt;
&lt;p&gt;Not a particularly astounding idea, admittedly. I just thought it was a neat trick, and wanted to share it.&lt;/p&gt;</description></item><item><title>Visual State in Silverlight Control Templates</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/06/10/visual-state</guid><link>http://www.interact-sw.co.uk/iangblog/2008/06/10/visual-state</link><pubDate>Tue, 10 Jun 2008 20:38:46 GMT</pubDate><description>&lt;p&gt;&lt;a href="http://silverlight.net/GetStarted/"&gt;Silverlight 2&lt;/a&gt; introduces a mechanism for customizing the dynamic aspects of a control’s appearance. This new model appeared for the first time in the &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/06/06/silverlight-2-beta2-released.aspx"&gt;recent beta 2 release&lt;/a&gt;, so I thought I’d take a crack at explaining it.&lt;/p&gt;
&lt;h3&gt;The Role of Visual States&lt;/h3&gt;
&lt;p&gt;In both Silverlight and WPF, a control’s appearance can be customized by providing a ‘template’. Templates are typically written in Xaml, and all the usual visual elements are available. But it’s usually not enough just to define the static structure of a control’s visuals.&lt;/p&gt;
&lt;p&gt;Most controls change their appearance when the user interacts with them. We expect elements to signal their ability to respond to input with subtle visual shifts when the mouse moves over them, for example. And this is where Silverlight’s ‘visual states’ come in. You can associate animations with visual state changes.&lt;/p&gt;
&lt;p&gt;(If you know WPF you’ll be familiar with triggers, which enable exactly this sort of connection between interaction and visuals. In an omission some WPF veterans find surprising, Silverlight doesn’t support triggers. Visual states are what we use instead.)&lt;/p&gt;
&lt;h3&gt;States and State Groups&lt;/h3&gt;
&lt;p&gt;To work with Silverlight’s visual state system, we must get to grips with &lt;i&gt;states&lt;/i&gt; and &lt;i&gt;state group&lt;/i&gt;&lt;i&gt;s&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;A control might have many facets, each with its own visual representation. For example, a checkbox may be checked, or unchecked. And it may also be focused or unfocused. The checked/unchecked state is wholly independent of the focused/unfocused state: the keyboard focus can move into and out of the control without changing the checked state. But not all states can be freely combined – obviously a checkbox cannot be both checked and unchecked (although it might be neither – there’s a third, ‘indeterminate’ state, but that’s mutually exclusive with both checked and unchecked).&lt;/p&gt;
&lt;p&gt;To clarify how states may combine, states are partitioned into state groups. For example, &lt;code&gt;CheckBox&lt;/code&gt; defines three state groups:&lt;/p&gt;
&lt;table cellspacing="0" class="blgTb"&gt;
  &lt;tr&gt;
    &lt;th class="blgTbFstCol"&gt;Group Name&lt;/th&gt;
    &lt;th&gt;States&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;CommonStates&lt;/td&gt;
    &lt;td&gt;Normal, MouseOver, Pressed, Disabled&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;CheckStates&lt;/td&gt;
    &lt;td&gt;Checked, Unchecked, Indeterminate&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;FocusStates&lt;/td&gt;
    &lt;td&gt;Focused, ContentFocused, Unfocused&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;So if we ask “What is a &lt;code&gt;CheckBox&lt;/code&gt;’s state?” the answer comes in three parts: a state from the &lt;code&gt;CommonStates&lt;/code&gt; state group, one from &lt;code&gt;CheckStates&lt;/code&gt;, and one from &lt;code&gt;FocusStates&lt;/code&gt;. A control effectively maintains multiple state machines, one for each state group. Here are some examples:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.interact-sw.co.uk/images/SilverlightCheckStates.png" alt="http://www.interact-sw.co.uk/images/SilverlightCheckStates.png"&gt;&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;CheckStates&lt;/code&gt; state group, all three checkboxes here are in the &lt;code&gt;Checked&lt;/code&gt; state. With respect to the &lt;code&gt;FocusStates&lt;/code&gt; state group, the first checkbox is in the &lt;code&gt;Focused&lt;/code&gt; state, while the second and third are in the &lt;code&gt;Unfocused&lt;/code&gt; state. Finally, as far as the &lt;code&gt;CommonStates&lt;/code&gt; state group is concerned, the first and third checkboxes are in the &lt;code&gt;Normal&lt;/code&gt; state, while the second is in the &lt;code&gt;MouseOver&lt;/code&gt; state. (The mouse cursor isn’t shown here, but you can see that the control has changed appearance.)&lt;/p&gt;
&lt;p&gt;A control will advertise the states it offers and the group to which each state belongs with the &lt;code&gt;TemplateVisualState&lt;/code&gt; attribute, as you can see here:&lt;/p&gt;
&lt;pre&gt;[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"ContentFocused"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"FocusStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"MouseOver"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CommonStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Focused"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"FocusStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Checked"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CheckStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Unchecked"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CheckStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Indeterminate"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CheckStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Pressed"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CommonStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Disabled"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CommonStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Unfocused"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"FocusStates"&lt;/span&gt;)]
[&lt;span style="color:dimgray"&gt;TemplateVisualState&lt;/span&gt;(Name = &lt;span style="color:brown"&gt;"Normal"&lt;/span&gt;, GroupName = &lt;span style="color:brown"&gt;"CommonStates"&lt;/span&gt;)]
&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;class&lt;/span&gt; &lt;span style="color:dimgray"&gt;CheckBox&lt;/span&gt; : &lt;span style="color:dimgray"&gt;ToggleButton &lt;/span&gt;...
&lt;/pre&gt;
&lt;p&gt;We now know that a control will maintain a simple state machine for each state group. So what? To do anything useful with this, we must use the &lt;code&gt;VisualStateManager&lt;/code&gt; class.&lt;/p&gt;
&lt;h3&gt;VisualStateManager&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;VisualStateManager&lt;/code&gt; class lets us connect animations to state changes. It defines an attached property we can use to establish these connections in Xaml.&lt;/p&gt;
&lt;p&gt;So that we can see this in action, let’s start with a simple template for a &lt;code&gt;CheckBox&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;CheckBox&lt;/span&gt;&lt;span style="color:red"&gt; Content&lt;/span&gt;&lt;span style="color:blue"&gt;="Check!"&lt;/span&gt;&lt;span style="color:red"&gt; IsChecked&lt;/span&gt;&lt;span style="color:blue"&gt;="True"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;CheckBox.Template&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Grid&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;          &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color:red"&gt; Width&lt;/span&gt;&lt;span style="color:blue"&gt;="19" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;          &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color:blue"&gt; /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateManager.VisualStateGroups&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateManager.VisualStateGroups&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Rectangle&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
         &lt;span style="color:red"&gt; Stroke&lt;/span&gt;&lt;span style="color:blue"&gt;="Blue"&lt;/span&gt;&lt;span style="color:red"&gt; Width&lt;/span&gt;&lt;span style="color:blue"&gt;="12"&lt;/span&gt;&lt;span style="color:red"&gt; Height&lt;/span&gt;&lt;span style="color:blue"&gt;="12"&lt;/span&gt;&lt;span style="color:red"&gt; HorizontalAlignment&lt;/span&gt;&lt;span style="color:blue"&gt;="Left"&lt;/span&gt;
         &lt;span style="color:red"&gt; RadiusX&lt;/span&gt;&lt;span style="color:blue"&gt;="2"&lt;/span&gt;&lt;span style="color:red"&gt; RadiusY&lt;/span&gt;&lt;span style="color:blue"&gt;="2"&lt;/span&gt;&lt;span style="color:red"&gt; Fill&lt;/span&gt;&lt;span style="color:blue"&gt;="Aqua" /&amp;gt;&lt;/span&gt;

&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Path&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="tick"&lt;/span&gt;&lt;span style="color:red"&gt; Opacity&lt;/span&gt;&lt;span style="color:blue"&gt;="0"&lt;/span&gt;
             &lt;span style="color:red"&gt; Width&lt;/span&gt;&lt;span style="color:blue"&gt;="12"&lt;/span&gt;&lt;span style="color:red"&gt; Height&lt;/span&gt;&lt;span style="color:blue"&gt;="12"&lt;/span&gt;&lt;span style="color:red"&gt; HorizontalAlignment&lt;/span&gt;&lt;span style="color:blue"&gt;="Left"&lt;/span&gt;
             &lt;span style="color:red"&gt; Stroke&lt;/span&gt;&lt;span style="color:blue"&gt;="Black"&lt;/span&gt;&lt;span style="color:red"&gt; StrokeThickness&lt;/span&gt;&lt;span style="color:blue"&gt;="2"&lt;/span&gt;
             &lt;span style="color:red"&gt; Data&lt;/span&gt;&lt;span style="color:blue"&gt;="M3,6 L5,9 9,2" /&amp;gt;&lt;/span&gt;

&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ContentPresenter&lt;/span&gt;&lt;span style="color:red"&gt; Grid.Column&lt;/span&gt;&lt;span style="color:blue"&gt;="1" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Grid&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;CheckBox.Template&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;CheckBox&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This defines some pretty basic visuals – just a simple box and tick, plus a placeholder. It looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.interact-sw.co.uk/images/SilverlightStatesCustom1.png" alt="http://www.interact-sw.co.uk/images/SilverlightStatesCustom1.png"&gt;&lt;/p&gt;
&lt;p&gt;Note that although I’ve got a &lt;code&gt;Path&lt;/code&gt; element in the shape of a tick, it’s currently invisible. Later I will supply a couple of animations to make the tick visible and invisible.&lt;/p&gt;
&lt;p&gt;Notice that the example above already contains an empty &lt;code&gt;&amp;lt;&lt;/code&gt;&lt;code&gt;vsm&lt;/code&gt;&lt;code&gt;:VisualStateManager.VisualStateGroups&lt;/code&gt;&lt;code&gt;&amp;gt;&lt;/code&gt; element – it’s just after the grid’s column definitions. I’ve been working with Xaml for half a decade now, but for anyone who hasn’t had that pleasure, I suppose it might not be totally obvious that this element sets a property on the containing &lt;code&gt;Grid&lt;/code&gt;! In general, Xaml elements of the form &lt;code&gt;&amp;lt;&lt;/code&gt;&lt;code&gt;Xxx.Yyy&lt;/code&gt;&lt;code&gt;&amp;gt;&lt;/code&gt; denote a property. The part after the ‘.’ is the property name, so that tells us that this is the &lt;code&gt;VisualStateGroups&lt;/code&gt; property. And the part before the ‘.’ is the name of the class that defines the property – so here we can see this property is defined by the &lt;code&gt;VisualStateManager&lt;/code&gt; class. Again, if you’re not familiar with Xaml, it may seem a bit funky that I can set property defined by &lt;code&gt;VisualStateManager&lt;/code&gt; on &lt;code&gt;Grid&lt;/code&gt;, a completely unrelated class. This is an example of a Xaml feature called &lt;i&gt;attached properties&lt;/i&gt;– they’re a way of bolting new properties onto existing types.&lt;/p&gt;
&lt;p&gt;For some reason the &lt;code&gt;VisualStateManager&lt;/code&gt; class isn’t in the Silverlight Xaml namespace, so we’ve had to qualify it with a namespace prefix. (I’ve chosen “vsm:” in this example, short for &lt;code&gt;VisualStateManager&lt;/code&gt;.) For this to work, I need to have defined that prefix somewhere in my Xaml. It’s not shown in the above snippet, but I put it at the root element, alongside the other namespace declarations:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:red"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;="clr-namespace:System.Windows;assembly=System.Windows"&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Normally you’d let Blend do all this for you. The June preview of Expression Blend 2.5 provides new features for working with the &lt;code&gt;VisualStateManager&lt;/code&gt;. But my goal here is to explain the underlying mechanisms, so I’ll steer clear of Blend for now.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;i&gt;Note&lt;/i&gt;&lt;/b&gt;&lt;i&gt;: in the Silverlight 2 beta 2 preview bits, the Visual Studio &lt;/i&gt;&lt;i&gt;Xaml&lt;/i&gt;&lt;i&gt; editor &lt;/i&gt;&lt;i&gt;reports &lt;/i&gt;&lt;i&gt;an error&lt;/i&gt;&lt;i&gt; on the line with this property. It says&lt;/i&gt;&lt;i&gt;: “The attachable property ‘&lt;/i&gt;&lt;i&gt;VisualStateGroups&lt;/i&gt;&lt;i&gt;’ was not found in type ‘&lt;/i&gt;&lt;i&gt;VisualStateManager&lt;/i&gt;&lt;i&gt;’.” &lt;/i&gt;&lt;i&gt;But it’s lying – the attachable property is present, and although you get what looks like an error in Visual Studio.&lt;/i&gt;&lt;i&gt; But you’ll find the project will happily build and run despite this &lt;/i&gt;&lt;i&gt;‘error’, and if you close the &lt;/i&gt;&lt;i&gt;Xaml&lt;/i&gt;&lt;i&gt; editor, the error goes away. It’s not really an error. That’s beta software for you.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;So what do we put inside this property? We can provide an animation to run for each state. On top of that, we can optionally get fancy, and provide animations that handle the transition between a specific pair of states. Let’s start with the simpler case.&lt;/p&gt;
&lt;h3&gt;Per-State Animations&lt;/h3&gt;
&lt;p&gt;The simplest way to use the visual state manager is to define an animation to run when a particular state is entered. The following example is about as simple as it gets:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateManager.VisualStateGroups&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="CommonStates"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="MouseOver"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColorAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
           &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="(Rectangle.Fill).&lt;/span&gt;
&lt;span style="color:blue"&gt;                                (SolidColorBrush.Color)"&lt;/span&gt;
            &lt;span style="color:red"&gt;To&lt;/span&gt;&lt;span style="color:blue"&gt;="PaleGreen"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0:0:0.5" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateManager.VisualStateGroups&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Notice that I am required to say first of all which &lt;i&gt;state group&lt;/i&gt; I’m interested in, with a &lt;code&gt;VisualStateGroup&lt;/code&gt; element. In this case, I’m looking at the &lt;code&gt;CommonStates&lt;/code&gt; group. And inside the group, I provide a &lt;code&gt;VisualState&lt;/code&gt; element for each state I wish to animate. To keep things simple, I’ve just handled one state here: &lt;code&gt;MouseOver&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With this markup in place, when the &lt;code&gt;CommonStates&lt;/code&gt; state group enters the &lt;code&gt;MouseOver&lt;/code&gt; state (i.e. when the user moves the mouse over the control), the &lt;code&gt;Storyboard&lt;/code&gt; I’ve supplied here will run. This particular animation changes the checkbox’s fill to pale green.&lt;/p&gt;
&lt;p&gt;Since I’ve only handled one state, the checkbox will get stuck in that state – I’ve not yet provided any animations to put it back how it was. And I’m also missing some other states, so to complete the job, I should add the following inside that &lt;code&gt;VisualStateGroup&lt;/code&gt; after the first &lt;code&gt;VisualState&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="Pressed"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColorAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
                   &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="(Rectangle.Fill).&lt;/span&gt;
&lt;span style="color:blue"&gt;                    (SolidColorBrush.Color)"&lt;/span&gt;
                   &lt;span style="color:red"&gt; To&lt;/span&gt;&lt;span style="color:blue"&gt;="Navy"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0:0:0.5" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="Disabled"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColorAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
                   &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="(Rectangle.Fill).&lt;/span&gt;
&lt;span style="color:blue"&gt;                    (SolidColorBrush.Color)"&lt;/span&gt;
                   &lt;span style="color:red"&gt; To&lt;/span&gt;&lt;span style="color:blue"&gt;="Gray"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0:0:0.5" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="Normal"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColorAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
                   &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="(Rectangle.Fill).&lt;/span&gt;
&lt;span style="color:blue"&gt;                    (SolidColorBrush.Color)"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0:0:0.5" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Note that the last of these doesn’t specify the &lt;code&gt;To&lt;/code&gt; attribute for the animation. As is usual for animations in Silverlight and WPF, this means ‘animate back to the base value’, which is exactly what you’d hope for when returning to the &lt;code&gt;Normal&lt;/code&gt; state.&lt;/p&gt;
&lt;p&gt;I’m going to use a similar but cruder trick to handle the checkbox state. The following goes after the first &lt;code&gt;VisualStateGroup&lt;/code&gt; element:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="CheckStates"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="Checked"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;DoubleAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tick"&lt;/span&gt; 
                      &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="Opacity"&lt;/span&gt;
                      &lt;span style="color:red"&gt; To&lt;/span&gt;&lt;span style="color:blue"&gt;="1"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="Unchecked" /&amp;gt;&lt;/span&gt;
&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;As before, this declares that I want to handle state changes for a particular state group – the &lt;code&gt;CheckStates&lt;/code&gt; group this time. And I’ve provided &lt;code&gt;VisualState&lt;/code&gt; elements to tell Silverlight what to do when entering either the &lt;code&gt;Checked&lt;/code&gt; or &lt;code&gt;Unchecked&lt;/code&gt; states.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Checked&lt;/code&gt; storyboard is slightly odd: it has a &lt;code&gt;Duration&lt;/code&gt; of 0. This is a case of trigger envy – in WPF I’d just write a simple trigger that says when &lt;code&gt;IsC&lt;/code&gt;&lt;code&gt;hecked&lt;/code&gt; is true, the ‘tick’ element should be visible. But Silverlight doesn’t have triggers, so instead I’ve had to use an animation that runs instantaneously to get the same effect.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Unchecked&lt;/code&gt; handling is even stranger: I’ve provided an empty &lt;code&gt;VisualState&lt;/code&gt; element. The significance of this is that I’m telling Silverlight that I care about this state, but that I have no new animations to run. And the important, non-obvious point is that when a new &lt;code&gt;VisualState&lt;/code&gt; element comes into play, Silverlight deactivates any animations that were previously in effect from other &lt;code&gt;VisualState&lt;/code&gt; elements for that state group.&lt;/p&gt;
&lt;p&gt;And even though my &lt;code&gt;Checked&lt;/code&gt; animation has a duration of 0, it remains active until we enter the &lt;code&gt;Unchecked&lt;/code&gt; state. Animations in Silverlight (and WPF) usually continue to have an effect even after they complete – if you animate something from red to green, it will remain green once the animation finishes. (You can change that if you like – this is controlled with the animation’s &lt;code&gt;FillBehavior&lt;/code&gt; property.) So in this case, the &lt;code&gt;Checked&lt;/code&gt; animation completes instantly, but remains active, holding the ‘tick’ element’s Opacity property at 1 until the animation is deactivated. That deactivation happens when we enter the &lt;code&gt;Unchecked&lt;/code&gt; state.&lt;/p&gt;
&lt;p&gt;So it’s quite common to see empty &lt;code&gt;VisualState&lt;/code&gt; elements. They’re usually present to deactivate any other animations for the state group.&lt;/p&gt;
&lt;p&gt;Note that the example so far is incomplete. I’ve not handled the &lt;code&gt;Indeterminate&lt;/code&gt; state in the &lt;code&gt;CheckStates&lt;/code&gt; group. And I’ve also not handled the &lt;code&gt;FocusStates&lt;/code&gt; state group at all. Nor do I plan to – I’m just trying to show the model here, and to fill in the gaps would mean more of the same.&lt;/p&gt;
&lt;p&gt;(If you want to see a fuller example, I’ve put focus state handling into the version available for download: &lt;a href="http://www.interact-sw.co.uk/downloads/SilverlightVisualStateDemo.zip"&gt;SilverlightVisualStateDemo.zip&lt;/a&gt;. If you want to run the application online, it’s up at &lt;a href="http://www.interact-sw.co.uk/slapps/visualstate/"&gt;http://www.interact-sw.co.uk/slapps/visualstate/&lt;/a&gt; Note that these versions also include the feature described in the next section.)&lt;/p&gt;
&lt;h3&gt;Transition Animations&lt;/h3&gt;
&lt;p&gt;Whereas the &lt;code&gt;VisualState&lt;/code&gt; element we’ve just seen defines the animation to use when entering a particular state, a transition animation is more specific: you can define the animation to run when transitioning between a particular pair of states.&lt;/p&gt;
&lt;p&gt;Note that transition animations do not replace the &lt;code&gt;VisualState&lt;/code&gt; animations. A transition animation runs &lt;i&gt;before&lt;/i&gt; the &lt;code&gt;VisualState&lt;/code&gt; animation. For example, suppose you’ve defined a transition animation for the transition from the &lt;code&gt;Pressed&lt;/code&gt; to &lt;code&gt;Normal&lt;/code&gt; states in the &lt;code&gt;CommonStates&lt;/code&gt; group. Silverlight will first run this transition animation, and once that has finished, it goes on to run the state animation.&lt;/p&gt;
&lt;p&gt;You might think that you could use transition animations as a replacement for state animations – why not provide only a transition, and leave out the final state animation? That turns out not to work: Silverlight requires you to specify the duration of your transition, and it will deactivate the transition animation at the end. And if you leave off the duration, the duration becomes 0 at which point the animation effectively never runs at all.&lt;/p&gt;
&lt;p&gt;This constraint ensures that no matter how a control entered a state, and no matter which transition animation ran as a result, it always ends up looking the same when it’s in that state, once all the animations have played out.&lt;/p&gt;
&lt;p&gt;To put it another way, Silverlight will always eventually run the animation for the target state, as specified in the relevant &lt;code&gt;VisualState&lt;/code&gt;, but before it runs that you get to insert something specific to the preceding state. This gives you just enough power to ensure that every possible state change looks coherent, while minimizing the scope for chaos.&lt;/p&gt;
&lt;p&gt;Here’s a simple and slightly contrived example:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="CommonStates"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup.Transitions&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualTransition&lt;/span&gt;&lt;span style="color:red"&gt; From&lt;/span&gt;&lt;span style="color:blue"&gt;="Pressed"&lt;/span&gt;&lt;span style="color:red"&gt; To&lt;/span&gt;&lt;span style="color:blue"&gt;="Normal"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="&lt;/span&gt;&lt;span style="color:blue"&gt;0:0:0.5&lt;/span&gt;&lt;span style="color:blue"&gt;"&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;        &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;ColorAnimation&lt;/span&gt;&lt;span style="color:red"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="color:blue"&gt;="tickBox"&lt;/span&gt;
                     &lt;span style="color:red"&gt; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="color:blue"&gt;="(Rectangle.Fill).&lt;/span&gt;
&lt;span style="color:blue"&gt;                      (SolidColorBrush.Color)"&lt;/span&gt;
                     &lt;span style="color:red"&gt; To&lt;/span&gt;&lt;span style="color:blue"&gt;="Red"&lt;/span&gt;&lt;span style="color:red"&gt; Duration&lt;/span&gt;&lt;span style="color:blue"&gt;="0:0:0.5" /&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;      &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;Storyboard&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;    &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualTransition&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualStateGroup.Transitions&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:brown"&gt;  &lt;/span&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:brown"&gt;vsm&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:brown"&gt;VisualState&lt;/span&gt;&lt;span style="color:red"&gt; x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;="MouseOver"&amp;gt;&lt;/span&gt;
    ... as before ...
&lt;/pre&gt;
&lt;p&gt;This has a &lt;code&gt;VisualStateGroup&lt;/code&gt; for the &lt;code&gt;CommonStates&lt;/code&gt; group as before, but I’ve added a &lt;code&gt;Transitions&lt;/code&gt; property. Inside this I’ve added a &lt;code&gt;VisualTransition&lt;/code&gt; defining an extra animation to be run prior to the animation for the &lt;code&gt;Normal&lt;/code&gt; state if we are coming into that state from the &lt;code&gt;Pressed&lt;/code&gt; state.&lt;/p&gt;
&lt;p&gt;So if the user presses the button down on the &lt;code&gt;CheckBox&lt;/code&gt;, and then moves away before letting go, the check box will briefly go red thanks to this transition animation, and then the &lt;code&gt;Normal&lt;/code&gt; state animation will run, returning the control back to its standard appearance.&lt;/p&gt;
&lt;p&gt;Note that you are allowed to leave off either the To, or From attributes from a &lt;code&gt;VisualTransition&lt;/code&gt;. This turns it into a kind of wildcard transition. With just a From attribute, the transition will run any time moving from the named state. With just a To attribute, it will run any time moving to the named state.&lt;/p&gt;
&lt;p&gt;You may wonder what is the point of a transition animation with just a To attribute. What does that offer that I can’t do more easily with a normal &lt;code&gt;VisualState&lt;/code&gt; animation? Well there is one subtle but useful difference. &lt;code&gt;VisualState&lt;/code&gt; animations run any time a state is entered, &lt;i&gt;even when the control first appears&lt;/i&gt;. Transition animations do not. Consider our checkbox example – you might want the tick to fade in and out rather than coming and going abruptly. You could get that effect by adding a non-zero duration to the &lt;code&gt;VisualState&lt;/code&gt; animation for the &lt;code&gt;Checked&lt;/code&gt; state shown earlier. However, if the checkbox happened to be checked when it first appeared, the animation would run then too, which might not be what you want. By contrast, a transition animation with To="Checked" will not run when the control first appears, but will run any time the control enters the Checked state from some other state.&lt;/p&gt;
&lt;p&gt;You can even leave off both To and From, in which case the transition will be inserted before every state change for that state group.&lt;/p&gt;
&lt;h3&gt;Designer Support&lt;/h3&gt;
&lt;p&gt;In summary, you get to define animations that run when the control enters a particular state, and you may also define specialized transition animations that run just before the main animation. It’s certainly different from WPF’s trigger mechanisms, and while it lets you achieve a lot of the same goals, it’s not quite as flexible – you only get to write state animations for the visual states the control author has decided to offer, whereas triggers can use any property.&lt;/p&gt;
&lt;p&gt;However, there’s one thing visual states do better than triggers: designer support. That’s not to say that WPF triggers aren’t supported by Blend – it does in fact support them. But you have to work harder with triggers. The benefit of the visual state model is that Blend can see exactly what states a control offers, and it can help a visual designer fill in all the necessary pieces. By contrast, triggers are so flexible that it’s difficult for a design tool to know where to start.&lt;/p&gt;
&lt;p&gt;If you’re happy cranking out Xaml by hand, you’ll probably miss triggers. But if you live in Blend, visual states offer useful benefits.&lt;/p&gt;
&lt;h3&gt;Learn More about Silverlight (Gratuitous Plug)&lt;/h3&gt;
&lt;p&gt;If you want to learn more about Silverlight, &lt;a href="http://www.pluralsight.com/"&gt;Pluralsight&lt;/a&gt; offers a couple of courses, for which I’m a co-author and instructor. There’s our main &lt;a href="http://www.pluralsight.com/courses/AppliedSilverlight.aspx"&gt;Silverlight course&lt;/a&gt;, recently updated for the Beta 2 release. And we also offer a dual feature &lt;a href="http://www.pluralsight.com/courses/DoubleFeatureAjaxSilverlight.aspx"&gt;Silverlight/ASP.NET Ajax course&lt;/a&gt;, running for more hours in the day and for more days, in which we cover both Silverlight and also ASP.NET Ajax. &lt;/p&gt;</description></item><item><title>Applied WPF Demos from London</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/05/13/wpf-demos-london</guid><link>http://www.interact-sw.co.uk/iangblog/2008/05/13/wpf-demos-london</link><pubDate>Tue, 13 May 2008 15:25:15 GMT</pubDate><description>&lt;p&gt;My apologies to everyone who attended my WPF course in London last month - it looks like I forgot to upload the demos. I'm afraid I confused myself by putting the wrong title on the previous week's demos...until today the &lt;a href="http://www.interact-sw.co.uk/iangblog/2008/04/03/silverlight-demos-london"&gt;Silverlight demos&lt;/a&gt; from the previous week had a title claiming to be WPF demos. Oops.&lt;/p&gt;
&lt;p&gt;So here, at least are the &lt;a href="http://www.interact-sw.co.uk/downloads/WpfDemosLondon2008-04.zip"&gt;WPF demos&lt;/a&gt; from the &lt;a href="http://www.pluralsight.com/courses/AppliedWPF.aspx"&gt;WPF course&lt;/a&gt; I ran last month.&lt;/p&gt;</description></item><item><title>Deep Zoom at the BBC</title><guid isPermaLink="true">http://www.interact-sw.co.uk/iangblog/2008/05/10/bbc-deep-zoom</guid><link>http://www.interact-sw.co.uk/iangblog/2008/05/10/bbc-deep-zoom</link><pubDate>Sat, 10 May 2008 14:45:50 GMT</pubDate><description>&lt;p&gt;The &lt;a href="http://www.bbc.co.uk/radio1/bigweekend/2008/zoom/"&gt;BBC has a Silverlight deep zoom app&lt;/a&gt; as part of the web coverage of the &lt;a href="http://www.bbc.co.uk/radio1/bigweekend/"&gt;‘Radio 1 Big Weekend’&lt;/a&gt; event. (“Europe’s biggest free ticketed event” apparently.) If you live in the UK and have watched BBC television at all this week, you’ve probably already seen the adverts for the event itself.&lt;/p&gt;
&lt;p&gt;The deep zoom app provides a flavour of the event for people who can’t turn up in person – photos of the event will be uploaded throughout the weekend. (And I guess people who did go could try and find themselves in the crowd shots next week.)&lt;/p&gt;
&lt;p&gt;From a technical perspective, it’s nothing you won’t already have seen if you’ve looked at the &lt;a href="http://memorabilia.hardrock.com/"&gt;Hard Rock Cafe’s memorabilia site&lt;/a&gt; – indeed, the BBC app is somewhat simpler. The main difference is that the BBC’s photo collection that will grow as the event unfolds – not quite live of course, but a bit less static than the Hard Rock application. (And more raw for it of course, but that’s probably in keeping with the nature of the event.)&lt;/p&gt;
&lt;h3&gt;Deep Zoom and Photo Collections&lt;/h3&gt;
&lt;p&gt;What I find interesting is that I like this way of browsing photos &lt;i&gt;much&lt;/i&gt; more than I thought I would. Deep zoom is definitely one of the more conspicuously fun features of Silverlight 2, but as it’s ultimately a bit of a one-trick pony, I thought I’d be pretty much done with it after playing with the Hard Rock demo.&lt;/p&gt;
&lt;p&gt;But it turns out still to be growing on me long after the initial ‘ooh’ factor. I think it’s a surprisingly natural way to present photographs.&lt;/p&gt;
&lt;p&gt;I just went to &lt;a href="http://www.flickr.com/"&gt;flickr&lt;/a&gt;, and coincidentally enough the first picture it showed me on the front page was &lt;a href="http://www.flickr.com/photos/notsogoodphotography/503637906/"&gt;a shot from a concert&lt;/a&gt;. It’s a good picture, but wow the user interface feels flat! Having got used to the deep zoom style of photo browsing, flickr now feels positively broken.&lt;/p&gt;
&lt;p&gt;So I want deep zoom for my online photo collections please!&lt;/p&gt;</description></item></channel></rss>