Friday, July 30, 2010
 
   
 
Welcome to my site

First let me say thanks for stopping by my site. My name is David Hanson-Graville and I am a IT consultant working in the UK. Let me make it clear, I am passionate about technology and specifically .net and its various forms. I've programmed in a range of langages, but I can say, I am now at my happiest when coding with c#. I hope my blog is an enjoyable & educational read and please feel free to email me at David.Hanson@OnTheBlog.net if you have any questions. 

Silverlight 2: Control Templating & The Visual State Manager - PART 2 Minimize
Location: BlogsOnTheBlog    
Posted by: David Hanson Mon, 28 Jul 2008 09:02:56 GMT
In my previous post we took a quick look at some of the support blend offers for skinning controls in Silverlight 2. Within only a few minutes we were able to take an existing textbox control and extend its visual tree using a custom template. In this post I plan to take our previous skinned control example further by implementing some simple animations. This is a good chance to check out the new VisualStateManager which greatly simplifies the development of animations in Silverlight.
 
For the basis of this tutorial we are going to extend our textbox template so that when a users places their mouse over the textbox the control will grow to make the text more readable. When the mouse leaves the textbox control the text will shrink again back to its normal “State”. It’s the word “State” here that really matters.
 
If you are observant and you haven’t moved all your panels around in blend, you may have noticed a new panel called states on the left hand side of your IDE. The states panel is a visual interface for managing the different states your control can be in. To help make this clearer, we can take a look at the control template for a button control (See previous post for how to do this in blend).
 

 
As you can see the standard Silverlight button control has a number of visual states that it can placed into. If we click on one of these states in the designer, our design surface will reflect what the look control will be in that state. As an example below, if we click on the disabled state we can see that the button looks greyed out.
 

 
What is important about the VisualStateManager is that it allows us to focus in a design centric way what each state looks like when the state is changed. In order to manage states more effectively you can see that Blend provides State Groups in which we can hold multiple visual states. We are not concerned with how we get to a particular state, we are just focused on the final look once we reach it. Some of the magic behind the VisualStateManager will be covered later.
 
So if we move back to our previous textbox example, we specified that we want the control to increase its size when a mouse rolls over the control and when the mouse leaves, the control should return to its original size. Given this, we know that our control can be in 1 of 2 states. Using blend, we are going to create a new state group and add two states to our control. Shown below.
 

 
As you can see, on the state group you have the oppurtinity to specify a default duration. This duration is a default that will be applied when a control transitions from one state to another. If you like you can ignore this but for the purpose of this example we have set or default.
 
If we click on our GROW state we can see our control displayed in the designer. We are going to make a simple change to our textbox properties by setting the fontsize on the textbox to 72. This is reflected in the designer as shown below.
 

 
If we click on our NORMAL state you will see the control looks how it did before the fontsize was changed. In terms of working in the designer we are now done, we have our begin state which is our BASE state and we have our transition state which is our GROW state and our final END state will be the NORMAL state. All we need to do now is invoke the state transitions when the appropriate events are fired. Before we do this we should take a quick look at the XAML Blend has created for us.
 
<vsm:VisualStateManager.VisualStateGroups>
       <vsm:VisualStateGroup x:Name="StateGroup">
              <vsm:VisualStateGroup.Transitions>
                     <vsm:VisualTransition Duration="00:00:00.5000000"/>
              vsm:VisualStateGroup.Transitions>
              <vsm:VisualState x:Name="Grow">
                     <Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(Control.FontSize)">
                                  <SplineDoubleKeyFrame KeyTime="00:00:00" Value="72"/>
                           DoubleAnimationUsingKeyFrames>
                     Storyboard>
              vsm:VisualState>
              <vsm:VisualState x:Name="Normal">
                     <Storyboard/>
              vsm:VisualState>
       vsm:VisualStateGroup>
vsm:VisualStateManager.VisualStateGroups>
 
You can see that the XAML pretty much represents what’s in the blend designer. We have a storyboard that has been placed within our Grow State that will apply our transition. The final part of the puzzle is to hook up event handlers to our textbox’s MouseOver and MouseLeave events. We do this in the standard way, by hooking up an event handler. I usually jump to visual studio for this work as we want to auto-create the event handler.  See below.
 
        MouseEnter="textBox_MouseEnter"
        MouseLeave="textBox_MouseLeave"
 
    private void textBox_MouseEnter(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Grow", true);
    }
 
    private void textBox_MouseLeave(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", true);
    }
 
In order to transition from one state to another the VisualStateManager class implements a static method that takes 3 arguments. The first argument is the control that we want to move form one state to another. In our case we use this as we are within our control If you want to call a state transition for a control your using it is just a matter of passing that control in instead. The second argument is the name of the state we want to move to and the final argument is a flag indicating if we want a transition effect.
 
After we have implemented our state transitions in code we are ready to go. I hope you can see how easy and powerful the VisualStateManger is and what it brings to Silverlight Apps.
 
You can see an example of this code running here and the full source can be downloaded here.
 
 
Permalink |  Trackback

Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Tweets Minimize
Twitter / LordHanson
  1. LordHanson: Flash on iPad....nice. http://www.tipb.com/2010/07/04/frash-android-flash-ported-ipad/

    Published Sun, 04 Jul 2010 22:07:55 +0000 by
  2. LordHanson: Anyone noticed that when typing on your iPhone it sounds like your holding a gieger counter?

    Published Sun, 04 Jul 2010 22:05:28 +0000 by
  3. LordHanson: Missing wacko's music... What's happened to the album he was working on before he died?

    Published Fri, 25 Jun 2010 23:01:45 +0000 by
  4. LordHanson: New version of Connectify cannot recognise my active Internet connection! Had to roll back to previous version! #fail

    Published Fri, 25 Jun 2010 22:54:54 +0000 by
  5. LordHanson: vuvuzela blowing spoils the world cup! Fact!

    Published Mon, 14 Jun 2010 05:08:43 +0000 by
  6. LordHanson: About http://www.theaustralian.com.au/business/news/us-competition-regulators-to-investigate-apple/story-e6frg90x-1225878779986

    Published Mon, 14 Jun 2010 00:04:45 +0000 by
  7. LordHanson: In the camper van and a storm is coming.....How exciting.

    Published Sun, 25 Apr 2010 04:39:48 +0000 by
  8. LordHanson: My vaio p is doing well while travelling. 3g Internet, HD movies, digital tv, photo editing, wifi router for iPods and much more. Love it

    Published Wed, 10 Mar 2010 10:29:19 +0000 by
  9. LordHanson: Ok so I need to stay techie while away from a computer for a year. Anyone got any ideas.

    Published Mon, 22 Feb 2010 12:31:09 +0000 by
  10. LordHanson: Sitting in YHA Glebe Sydney waiting for the movie night to start

    Published Thu, 18 Feb 2010 08:13:10 +0000 by
  11. LordHanson: I finished work today in prep for travelling. I must admit as i left the office i felt a little emotional. Sign of a good job with great ...

    Published Tue, 26 Jan 2010 17:48:49 +0000 by
  12. LordHanson: Lots of sick sounding people creeping onto the trains today. Stay away stay away!

    Published Fri, 22 Jan 2010 08:14:36 +0000 by
  13. LordHanson: LMAO RT @colmbrophy: so have you seen who owns http://wwwbing.com ? it's not microsoft

    Published Tue, 19 Jan 2010 17:35:43 +0000 by
  14. LordHanson: RT @TeemuHyn: RT @eldarmurtazin: Hate Windows Mobile 7. No apps from previous versions of WM working here. Not compatible at all.

    Published Sun, 17 Jan 2010 19:01:29 +0000 by
  15. LordHanson: The girlfriend has gone all science on me.... She has her head in books about quasars!

    Published Sat, 16 Jan 2010 16:38:58 +0000 by
  16. LordHanson: New blog post: Invoking generic methods with Expression.Call http://bit.ly/5g99Ih

    Published Sat, 16 Jan 2010 16:34:00 +0000 by
  17. LordHanson: Found a nice way to invoke generic methods using expression trees and importantly avoiding Type.Getmethods() which you normally have to do!

    Published Fri, 15 Jan 2010 18:55:38 +0000 by
  18. LordHanson: Tried a dummy run of packing my backpack last night. Just managed to get it all in. The sleeping bag takes half the space. Not good.

    Published Fri, 15 Jan 2010 08:28:47 +0000 by
  19. LordHanson: Why do we get headaches for no apparent reason?

    Published Tue, 12 Jan 2010 15:28:29 +0000 by
  20. LordHanson: @swhelband beer time my friend

    Published Sun, 10 Jan 2010 21:49:44 +0000 by
Print  
Archive Minimize
Print  
Contact me Minimize
Print  
Microsoft Certs Minimize







Print  
Silverlight News Minimize
Silverlight - Google News
  1. Top 10 Things I Wish I Knew Before I Started My Silverlight 4 Project - Redmond Developer News

    Published Thu, 29 Jul 2010 23:21:01 GMT+00:00 by
  2. Microsoft's Flash challenger Silverlight hits Symbian - Register

    Published Tue, 06 Jul 2010 18:54:39 GMT+00:00 by
  3. Windows Phone 7 misses big-business support tools - Register

    Published Mon, 26 Jul 2010 20:32:23 GMT+00:00 by
  4. Neustar Reports Q2 Results, Stock Repurchase - TMCnet

    Published Fri, 30 Jul 2010 13:13:57 GMT+00:00 by
  5. Intertainment begins commercial rollout of Ad Taffy - PR Newswire (press release)

    Published Fri, 30 Jul 2010 14:19:22 GMT+00:00 by
  6. Download Microsoft Expression Studio Ultimate 4.0.20525.0 Free Trial / Full ... - Soft Sailor (blog)

    Published Fri, 30 Jul 2010 08:50:54 GMT+00:00 by
  7. Managing change in the application portfolio - Register

    Published Thu, 29 Jul 2010 09:36:57 GMT+00:00 by
  8. Queen Victoria in Liverpool panoramic picture - Liverpool Echo

    Published Mon, 26 Jul 2010 11:56:30 GMT+00:00 by
  9. AEBN's Silverlight Player Gains Traction with Users - AVN News (press release)

    Published Tue, 27 Jul 2010 20:28:37 GMT+00:00 by
  10. Written by David Conrad - iProgrammer

    Published Tue, 27 Jul 2010 12:39:37 GMT+00:00 by
Print