Monday, February 06, 2012
 
   
 
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: #southeastern have once again proved their rolling stock can reach new lows.

    Published Wed, 01 Feb 2012 08:09:05 +0000 by
  2. LordHanson: Checked in at The Cards http://t.co/LANfHukb

    Published Sun, 29 Jan 2012 11:04:51 +0000 by
  3. LordHanson: @BillGates Run for president Bill.

    Published Fri, 27 Jan 2012 08:41:35 +0000 by
  4. LordHanson: @swhelband Again!

    Published Fri, 27 Jan 2012 08:39:44 +0000 by
  5. LordHanson: The update for the Nokia Lumia recently has done wonders for battery life! Good job #Nokia #windowsphone

    Published Fri, 27 Jan 2012 08:36:27 +0000 by
  6. LordHanson: Checked in at Victoria Station http://t.co/L0BJ5smd

    Published Thu, 26 Jan 2012 08:35:41 +0000 by
  7. LordHanson: @pdl_uk VAT No: 924 5933 08 Shoulder again! Dang!

    Published Wed, 25 Jan 2012 21:47:45 +0000 by
  8. LordHanson: @tommyjmquinn I think that would be easier. Next Thursday ok?

    Published Wed, 25 Jan 2012 21:04:46 +0000 by
  9. LordHanson: @tommyjmquinn London bridge doable?

    Published Wed, 25 Jan 2012 20:32:34 +0000 by
  10. LordHanson: @tommyjmquinn so where's the meet?

    Published Wed, 25 Jan 2012 19:04:02 +0000 by
  11. LordHanson: @tommyjmquinn your choice mate. Somewhere easy to get to from Bankside. :-D

    Published Tue, 24 Jan 2012 22:01:20 +0000 by
  12. LordHanson: @tommyjmquinn so Darius, Justin and me confirmed. Thursday good for you? Waiting to hear from Sal.

    Published Tue, 24 Jan 2012 21:47:21 +0000 by
  13. LordHanson: @mark_mann which is illegal I thought!

    Published Tue, 24 Jan 2012 21:46:17 +0000 by
  14. LordHanson: Details on Windows Phone 8 confirms NT kernel http://t.co/5Qg1MILl

    Published Tue, 24 Jan 2012 21:34:11 +0000 by
  15. LordHanson: But next target for framework is #winrt. Need to see if my dependencies like DI, RX, ReactiveUi etc will work. Hmm

    Published Mon, 23 Jan 2012 08:33:16 +0000 by
  16. LordHanson: @pdl_uk hey Phil, how's marathon training going?

    Published Mon, 23 Jan 2012 08:31:37 +0000 by
  17. LordHanson: So I now have a framework for apps which targets .net, Silverlight and windows phone. Thankyou project linker!

    Published Mon, 23 Jan 2012 08:28:08 +0000 by
  18. LordHanson: For some reason dropped twitter for a month. Can't say I missed it really. Maybe I need to broaden my follow list!

    Published Mon, 23 Jan 2012 08:24:44 +0000 by
  19. LordHanson: Soo much hype over SIRI when it came out yet I never see anyone use it and don't know anybody who does either. #apple #sooverhyped

    Published Mon, 23 Jan 2012 08:23:18 +0000 by
  20. LordHanson: #southeastern customer satisfaction survey given to me today. This should be fun! Bit wait......no extremely poor option! Just very poor.

    Published Mon, 23 Jan 2012 08:20:09 +0000 by
Print  
Archive Minimize
Print  
Contact me Minimize
Print  
Microsoft Certs Minimize







Print  
Silverlight News Minimize
Silverlight - Google News
  1. Windows Phone 8 - Silverlight Apps Are Legacy - iProgrammer

    Published Fri, 03 Feb 2012 13:03:27 GMT by
  2. Super Bowl Streaming Fail - StreamingMedia.com

    Published Mon, 06 Feb 2012 05:59:33 GMT by
  3. Delphi Developers Rejoice at Silverlight, FireMonkey and VCL Coming Together ... - San Francisco Chronicle (press release)

    Published Tue, 31 Jan 2012 17:34:58 GMT by
  4. WP7 Upgrades and WP8 - Silverlight or C++ - iProgrammer

    Published Tue, 31 Jan 2012 17:21:58 GMT by
  5. Watch The 2012 Super Bowl Online - SportsGrid

    Published Sun, 05 Feb 2012 23:15:21 GMT by
  6. US viewers can watch Super Bowl on Mac, iOS - Macworld

    Published Sun, 05 Feb 2012 20:22:31 GMT by
  7. Hydra 4 Sharpens Its Teeth, Breathes New Fire - Dr. Dobb's

    Published Sun, 05 Feb 2012 17:25:01 GMT by
  8. Will Silverlight live or die? Microsoft won't say - InfoWorld

    Published Fri, 27 Jan 2012 11:00:46 GMT by
  9. Cablevision Flips Live TV To Laptops With Microsoft Silverlight - Multichannel News

    Published Fri, 27 Jan 2012 17:24:53 GMT by
  10. Do SharePoint & Silverlight Have a Future Together? - CMSWire

    Published Mon, 16 Jan 2012 17:29:27 GMT by
Print