Tuesday, February 09, 2010    
Blog  
OpenLight Blog
Jul 18

Written by: Michael Washington
7/18/2009 6:43 PM 

After seeing this demo: [Mini-Tutorial] Blend 3: Rich Interactivity with No Code and the cool way she was able to simply drop a "Behavior" on a button and a menu displays (and allows you to set some properties using simple drop downs no less) and then the button is now able to make another element move, I had to learn how to make behaviors.

There are examples on the web but all the ones I found were based on the Silverlight 3 preview and with the released version of Silverlight 3, there have been changes.

I decided that for my first behavior I would start with something extremely simple. I wanted to make a behavior that you could drop on one element, and when that element triggered the behavior, the text in a TextBox would change to lowercase.

This assumes you have Visual Studio installed. Also install:

Open Visual Studio and select File then New Project.

image

Select the Silverlight Application template.

image

Use the option to create a website to host the project.

image

Visual Studio will create a Solution. Right-click on the solution in the Solution Explorer and select Add then New Project...

image

This time create a Silverlight Class Library.

image

In the Silverlight Class Library, add a reference to the System.Windows.Interactivity.dll assembly located at:

C:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\Silverlight\System.Windows.Interactivity.dll

Rename the Class1.cs file that is automatically created to "MakeLowercase.cs".

Replace all the code in the MakeLowercase.cs file with:

Build the project.

image

In the main Silverlight Project (not the Silverlight Class Library that you just created the behavior in), add a reference to the Silverlight Class Library that you just created the behavior in.

Build the entire Solution.

image

Right-click on MainPage.xaml and select Open in Expression Blend...

image

In Expression Blend, add a Button and a TextBox to the design surface of MainPage.xaml.

image

Click the Assets button.

image

Select the Behaviors group and you will see the MakeLowercase behavior.

Drag and drop the MakeLowercase behavior onto the Button.

image

In the Objects and Timeline window, the MakeLowercase behavior will be attached to the Button.

image

When you click on the MakeLowercase behavior in the Objects and Timeline window and look at the Properties window, you will see that the EventName in the Trigger section is set to Click. This means the behavior will fire when the Button is Clicked.

In the Common Properties section, the TargetName is set to the default value of [Parent]. This wont work because the target must be a TextBox. You can click the round "bullseye" symbol next to [Parent] to enable the Artboard element picker.

image

You can then click on the TextBox and a popup will display indicating the TextBox will be given a name. Expression Blend is smart enough to know that the code wont work unless the TextBox has a name so it does it for you.

image

In the Common Properties section, the TargetName is now set correctly.

Save the page.

image

Switch back to Visual Studio and right-click on the .html test page in the web project and select Set As Start Page. Also right-click on the web project and select Set as StartUp Project.

Hit F5 to build and run the project.

image

You can type in the TextBox

image

When you click the Button, the text will change to lowercase.

You can see the live example at this link.

You can download the source code at this link.

Tags:

15 comment(s) so far...

Re: Creating a simple Silverlight 3 Behavior

Nice, Very Nice.
Thank you
Mike Greenway

By Mike Greenway on   7/21/2009 9:28 AM

Re: Creating a simple Silverlight 3 Behavior

Thanks so much for the walk-through. Just for fun I went a step further and "improved" the behavior so that if you type the text "UpperCase" it changes the text to "lowercase" (not the just the casing from upper to lower). Here's the code for clarification to keep my comment from sounding like an Abbott and Costello routine! :)

Protected Overrides Sub Invoke(ByVal parameter As Object)

Target.Text = Target.Text.ToLower

If Target.Text = "uppercase" Then
Target.Text = "lowercase"
End If

Target.Background = New SolidColorBrush(Colors.Red)

End Sub

By Rob on   7/22/2009 5:26 PM

Re: Creating a simple Silverlight 3 Behavior

@Mike - Thanks for the feedback!

By Michael Washington on   7/22/2009 5:24 PM

Re: Creating a simple Silverlight 3 Behavior

@Rob - Cool Thanks!

By Michael Washington on   7/22/2009 5:25 PM

Re: Creating a simple Silverlight 3 Behavior

Awesome as usual Michael. Your tutorials are great!

By Randy on   8/19/2009 6:55 PM

Re: Creating a simple Silverlight 3 Behavior

hi i am new to silverlight i been trying to find abt behaviours from lot of resources but the best tutorial i found is this
thanks a lot

By SANTHU on   8/27/2009 10:21 PM

Re: Creating a simple Silverlight 3 Behavior

thanks for your post. this is Great~

I wonder if I wanna do the same thing With Two TextBoxes.
(or One TextBox, and One PasswordBox)

By Shorty.Kei on   9/21/2009 12:04 AM

Re: Creating a simple Silverlight 3 Behavior

hi Michael,
tx for sharing.

i want to create a time picker control in silverlight 3 without a third party dlll or nay contolr ...can you help for this...

By malcolm on   9/28/2009 6:17 AM

Re: Creating a simple Silverlight 3 Behavior

@malcolm - I have not used the Time Picker yet

By Michael Washington on   9/28/2009 6:23 AM

Re: Creating a simple Silverlight 3 Behavior

Your html sample -- I think -- is missing a piece (the part (text within angled brackets) is not displaying)

Should it not read:

// TargetedTriggerAction means that while this behavior
// can be attached to any element, the TARGET can only be a TextBox
public class MakeLowercase : TargetedTriggerAction ....

By Sky on   10/18/2009 12:00 AM

Re: Creating a simple Silverlight 3 Behavior

Ha...it took out from the comment as well...let me try that again:

// TargetedTriggerAction means that while this behavior
// can be attached to any element, the TARGET can only be a TextBox
public class MakeLowercase : TargetedTriggerAction {...}

By Sky on   10/18/2009 12:02 AM

Re: Creating a simple Silverlight 3 Behavior

Wow. I give up trying to type it in (your comment box hides both angled brackets, and escaped angled brackets...)

What I was trying to say was that TargetedTriggerAction is a generic class...and between Angled brackets one would put the Type of the intended Behavior target...in this case...TextBox.

Whew. (Aftert that experience of watching my comments get munched on twice, I somewhat feel like the postman who got his leg bitten by that greedy little mutt of a comment box..."Was just delivering the mail, ma'am!")

Night.

By Sky on   10/18/2009 12:06 AM

Re: Creating a simple Silverlight 3 Behavior

@Sky - Thank you for letting me know. I replaced the code sample with an image so it is now correct.

By Michael Washington on   10/18/2009 5:31 AM

Re: Creating a simple Silverlight 3 Behavior

What is my mistake? I get two errors. Declaration Expected(line 13) and sub Invoke cannot be declared 'Overrides' because it does not override a sub in a base class(line 19)

Imports System
Imports System.IO
Imports System.Text
Imports System.Windows
Imports System.Collections.Generic
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation

Imports Microsoft.Expression.Interactivity

13. Public Class SimpleSampleAction : TargetedTriggerAction

Protected Overrides Sub Invoke (ByVal parameter As Object )
MsgBox("FIRST ACTION!!!!")
End Sub

19. End Class

By Angelica on   11/18/2009 8:18 AM

Re: Creating a simple Silverlight 3 Behavior

@Angelica - Yeah VB is coded differently but I do not know the proper syntax. Sorry.

By Michael Washington on   11/18/2009 8:19 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use