Syndicate

News

My name's Marco De Sanctis and I'm an IT professional from Italy. This is my technical blog, about .NET and related application development and design technologies.

Download my Resume (.doc)

Recent Comments

10/23/2008 at 3:48 PM

sorry, my previous post was not complete... I trie...
by glondi

Read more...

9/22/2008 at 10:03 AM

Good one!!!Could you please post source code for t...
by Nisa

Read more...

9/10/2008 at 5:53 PM

I didn't even realize people were aware of the WPF...
by Mike Brown

Read more...

9/8/2008 at 1:22 AM

Marco,How are you handling scenarios where you nee...
by ctodd

Read more...

9/3/2008 at 1:00 AM

@ctodd: Hi,basically for the reasons I described a...
by Marco De Sanctis

Read more...

Recent Posts

Crunch mode is a pure waste of time, energy and money

7/31/2008 at 5:41 PM

Read more...

Double Click on the .sln file doesn't open Visual Studio on Vista

7/27/2008 at 9:02 AM

Read more...

Domain Model & Aggregates: when do master-detail associations happen?

7/22/2008 at 4:08 PM

Read more...

How I Got Started in Software Development

7/14/2008 at 12:16 AM

Read more...

Unleash the power of VisualStateManager with custom states

6/30/2008 at 12:12 AM

Read more...

Unleash the power of VisualStateManager with custom states

posted on Monday, June 30, 2008 12:12 AM | Filed Under [ Silverlight ]

Property triggers are the WPF feature I miss the most in Silverlight 2: I really liked writing behaviors in declarative style with XAML and Styles and it was somehow frustrating being forced to use C# code and event handling to achieve similar results in Silverlight.

Although it's not here for exactly the same puropose, VisualStateManager is a great improvement to fill the gap thanks to:

  • Blend visual support
  • automatic state transition animation

Those two features, combined with the opportunity to inject custom states at your own will, make VisualStateManager so poweful that you're going to miss it once back to WPF, at least until it won't be supported for smart clients too.

Let's suppose I want to animate a textbox to change its background color when mouse pointer overs it. If I was in WPF I would have attached a trigger to the IsMouseOver property, in Silverlight we got no triggers, but we have Blend and VisualStateManager.

So what I do is putting a TextBox in my UserControl and ask Blend to customize its template:

image

As we can see, we got no states for the standard TextBox so VisualStateManager seems to be unuseful at first: should we once againf rely on procedural code to achieve our goal? Not at all (or, more exactly, almost not at all). Even though standard TextBox has no built-in states (it doesn't specify any in its attributes nor it invokes any state transition, as, for example, Button does) we can easily add our own simply editing the generated style XAML markup.

What we need is a custom StateGroup, called MouseStates, and a couple of states, Normal and MouseOver, so what we do is opening Visual Studio to have intellisense while typing, adding a reference to System.Windows namespace and writing the following lines of code:

image

Visual Studio complains about using VisualStateManager.VisualStateGroups attached property; however, don't care about it, everything will compile without errors. Ok, now back in blend to continue editing the control template:

image

As you can notice, Blend recognizes the 2 newly added states and suddenly adds them to the States panel. Now, we can animate the TextBox background as we would usually do:

image

Now we only need one last step: adding some code to invoke state transitions when the mouse pointer overs or moves away from the TextBox. So, back in Visual Studio, let's add a couple of event handlers:

image

Now, if you build and run the project, you'll notice how the TextBox gracefully changes its background color from white to shaded red. However, our textbox won't work in another UserControl unless we don't add the same event handling code to it. It would be preferrable to build a custom StateTextBox and internally hiding all that logic. Doing it is extremely easy, as it consists only on creating a class which inherits form Textbox and writing code similar to the previous one:

image

Notice the two attributes I added to the class declaration to describe the control's state machine structure; Blend is supposed to read them to show controls states when editing its template. Unfortunately, (as I wrote some days ago) Blend June CTP current version has template editing capabilities only for controls which belong to System.Windows.dll. It means that even if we use StateTextBox, we'll still need to manually do all the stuff. However, it still has the advantage to leave the page code behind free from any textbox state transition code.

As usual, source code is available here.

kick it on DotNetKicks.com

Comments

Gravatar
# re: Unleash the power of VisualStateManager with custom states
Posted by Sudhir Malireddy on 7/28/2008 12:59 PM
Very nice concept and can i expect for list box? , i am unable to bind visualstatemanager by code.
can u help me.
Gravatar
# re: Unleash the power of VisualStateManager with custom states
Posted by Marco De Sanctis on 7/28/2008 2:46 PM
Hi...

What kind of problem are you experiencing?
Gravatar
# Blowjobs.
Posted by Blowjobs. on 11/20/2008 3:41 PM
Blowjobs. Extreme blowjobs. Latina blowjobs. Asian blowjobs.
Gravatar
# Zoo sex tube.
Posted by Zoo sex tube. on 12/1/2008 9:04 PM
Zoo sex tube.
Gravatar
# Carmen electra having sex nudity.
Posted by Carmen electra topless. on 12/17/2008 9:48 AM
Carmen electra sex shots. Carmen electra nude. Carmen electra joan jett. Carmen electra naked. Carmen electra topless.
Gravatar
# Sex animal.
Posted by Free animal sex free. on 12/18/2008 10:15 AM
Hardcore animal sex. Animal sex. Farm animal sex.
Gravatar
# Meridia.
Posted by Does meridia work. on 12/21/2008 7:57 AM
Buy meridia meridia reductil mg generic e. Meridia diet pill. Meridia. Meridia back pain. Meridia without prescriptions. Meridia side effects.
Gravatar
# Toilet slave video.
Posted by Toilet slave video. on 12/25/2008 9:27 PM
Toilet slave video.
Gravatar
# Rape sites.
Posted by Youtube nirvana rape me. on 12/26/2008 8:20 AM
Rape video. Rape stories. Rape movie. Forced rape.
Gravatar
# Hardcore lesbian sex.
Posted by Free lesbian porn. on 12/26/2008 11:39 AM
Disney lesbian toons. Lesbian movies.
Gravatar
# Meridia diet index dietlist net.
Posted by Prescription weight loss medications - meridia. on 12/26/2008 5:21 PM
Buying meridia in mexico. Buy meridia meridia reductil mg generic e. Meridia meridia. Meridia. Regular pharmacy price for meridia. Meridia side effects.
Gravatar
# Jessica simpson.
Posted by Jessica simpson. on 12/27/2008 9:42 PM
Jessica simpson.
Comments have been closed on this topic.