Welcome to WindowsClient.net | Sign in | Join

Faisal's Blog

Sponsors





  • advertise here

Articles

Blogroll

April 2009 - Posts

First Experience With Behaviors In Silverlight 3

Introduction

Behaviors is a new feature included in Silverlight 3 to solve the problems of not being able to take full advantage of Triggers in the previous versions of Silverlight. We used Triggers in WPF which provided us the ability to change the look and feel of UIElements dynamically based on the state of the application declaratively or from Xaml. Behaviors will give you the same power as triggers and actions and little more. It works for WPF also. It is possible for designers to use Behaviors right on the Blend art board. Behaviors can be applied to elements with drag drop. Those who are designers wearing a developers hat or some king of integrator, can create new Behaviors in code. Once created the Behaviors show up in the Expression Blend 3 asset tool. You can use Behaviors for opening and closing dialogs, running animations, setting properties, validating input or navigating between screens.

Let’s Delve into an example.

Behaviors Example

You need to install Silverlight 3 and Microsoft Expression Blend 3 Preview in order to get started with Behaviors. I’m assuming that these are installed in your machine. Open Microsoft Expression Blend 3 Preview and select New Project from File menu :

New Project In Blend 3

Now add a new Class right clicking over the project name and selecting Add New Item. This will give you the options of Adding Items in your project by bringing up New Item dialog box :

Adding New Items In Project

Select the option Class in the New Item dialog box. Name the class anything you like. This depends on what type of Behaviors you’re going to create. In this case I’ve named this MessageBoxBehavior. Since we’re going to experience a MessageBox on Button click. Now you need to add reference Microsoft.Expression. Interactivity from your Blend install directory. This will provide the base classes that your behavior and action will subclass.

Your newly created class will inherit from TargetedTriggerAction which is an action that can be set up to target a different object than the object it is attached to. In this case I’m using TargetedTriggerAction<FrameworkElement > . To tweak the the behavior of elements you need to set up some values to match your requirements. This can be done via depedency properties. At first create a dependency property named DisplayTextProperty which will serve the purpose of showing text in a MessageBox.

public static readonly DependencyProperty DisplayTextProperty = 
DependencyProperty.Register("DisplayText", typeof(string),
           typeof(MessageBoxBehavior), null);

To expose the properties in Blend add the Category attribute to the property.

[Category("DisplayText")]
 public string DisplayText
 {
    get{return (string)GetValue(DisplayTextProperty);}
    set{SetValue(DisplayTextProperty,value);}
 }

Now all your action needs to do is to override the Invoke method.

protected override void Invoke(object parameter)
{
  DisplayText = "This is a Behavior. New Feature of Silverlight.";
  MessageBox.Show(DisplayText);
            
}

Now get back to the MainControl.xaml. Add a Button and name it anything you like. With button selected in the Objects and Timeline panel, go to the Asset Library and select the behavior tab . You’ll be able to see the behavior you created. Which is MessageBoxBehavior.

Behavior In Asset Library

Drag the MessageBoxBehavior over the Button. Now select the Behavior in the Objects and Timeline panel to change the default EventName at the properties.

BehaviorPropertiesAndEventsTab

You’ll be able to see that Loaded event is set by default. Change that to Click. since we want to see the MessageBox on Button click. Run the application by pressing F5. You’ll be able to see the MessageBox on every button click like the screen shot below.

Behavior after debugging

That's it for now. I’m still trying to do more with Behaviors. Hopefully I’ll post soon more on Behaviors in the next two weeks. Till then leave your comments and suggestion.

Shout it
Posted: Apr 29 2009, 08:53 PM by ilves | with 2 comment(s)
Filed under:
Page view counter