Windows Phone From Scratch #12–Custom Behaviors (Part I)

To fully understand how behaviors work, you need to create a couple customNottub Behaviors of your own.  In this tutorial we’ll build a custom behavior and in the next we’ll look at a specialized form of custom behavior called a TriggerAction.

To begin, open the previous project and add a second button next to the OK button. Leave the contents as Button or set them to anything you like (see image)

Next, open the project in Visual Studio and create a new class named ReverseAction.cs.  Add the following code:

 

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace WPFS12
{
   public class ReverseAction : Behavior<Button>
   {
      protected override void OnAttached()
      {
         base.OnAttached();
       }

      protected override void OnDetaching()
      {
         base.OnDetaching();
      }
   }
}

So far, you’re just inheriting from the base class Behavior (in the System.Windows.Interactivity namespace) and you are overriding the two methods of that base class: On Attached and OnDetaching.  As you can guess, the first is called when the action is attached to a control, the second when the action is unattached from the control.

In this case, we want to take an action each time the button is clicked, so let’s hook the click event (being careful to unregister when we detach).

protected override void OnAttached()
{
   base.OnAttached();
   AssociatedObject.Click += AssociatedObject_Click;
}

protected override void OnDetaching()
{
   base.OnDetaching();
   AssociatedObject.Click -= AssociatedObject_Click;
}

All the action is in the event handler, where we will get the current contents of the button and reverse the string, writing it back to the button (we know it is a button as we’ve designated this a Behavior<Button> and so only buttons can use this behavior).

void AssociatedObject_Click( object sender, RoutedEventArgs e )
{
   var currentContents = AssociatedObject.Content.ToString();
   var newStringBuilder = new StringBuilder();
   for ( var i = currentContents.Length - 1; i >= 0; i-- )
   {
      newStringBuilder.Append( currentContents[ i ] );
   }
   AssociatedObject.Content = newStringBuilder.ToString();
}

I fully admit that this is an action of limited utility; but it does demonstrate everything there is to know about creating a basic custom action.  BuildReverse the  application and then switch back to Blend.  Build the application in Blend and then check the Assets->Behavior tab; you should find your new Action listed with the standard actions.

Drag the new action onto your new, second button and build and run the application – each time you click the button the action is applied and the text of the button is reversed.

Hooking an event is so common, there is an idiom for it using the TriggerAction<T> which we’ll discuss in the next tutorial.

Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in Blend, Essentials, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

One Response to Windows Phone From Scratch #12–Custom Behaviors (Part I)

  1. om says:

    Great this was helpful…Thank you.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>