The Binding object only can take DependencyProperty on the FrameworkElement as a binding target in Silverlight. ‘Course, all of methods and properties related to DataBinding is on the FrameworkElement. DataBinding model might be designed to bind a source data to target ‘UI Element’ at the first time, I guess. It’s reasonable, and it looks like flawless. Just Before Behaviors feature was added to Silverlight.

Oneday, I created a simple Silverlight 3 application to explain Behaviors for my coworkers with Silverlight 3. Me and my coworkers were quite familiar with DataBinding through expierence of Silverlight 2, so I wanted to show them a demo with DataBinding.

The behavior what I created was very simple, you might already know ‘ShowMessageBoxAction’ if you tried Silverlight 3 Behaviors. Here’s the code:

 public class ShowMessageBoxAction : TriggerAction<DependencyObject>
{
    public string Message { get; set; }

    protected override void Invoke(object parameter)
    {
        MessageBox.Show(Message);
    }
}

Yes it was a triggered action which shows MessageBox when Click event raised on Button. Nothing special, isn’t it? And then I wanted to mix it up DataBinding like:

<Button Content="Description">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <local:ShowMessageBoxAction Message="{Binding Description}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</
Button>

If you are a developer who is familiar with DatatBinding, you might be aware of my mistake and it would never work. As I mentioned above, a target of DataBinding must be a DependencyProperty on FrameworkElement. I think I got out of my mind at that time. It’s absolutely my mistake because all the Triggers and Behaviors were not FrameworkElement so these were not parts of VisualTree.

However, was that approach bad or evil? Well, I don’t think so. Because it looks like natural. I mean, when we write some XAML codes, It’s not easy to be aware of whether an element(tag) is a FrameworkElement or just other AttachedProperty. And it’s almost impossible to explain it reasonably for designers. Imagine that how designers add some behaviors to an element by using Expression Blend, Behaviors will shown on ‘Object Tree Panel’, not a just Property Panel. Who could know it’s impossible to use DataBinding untill opened a property panel of a Behavior and realize that DataBinding property was disabled.

What do I want to say? Well, I’m understand why Behaviors can’t be binded using DataBinding. However I think DataBinding makes work easy and efficeint, so how about if DataBinding could reach through any DependencyObject or AttachedProperty element?


Posted by gongdo

Submit comment.

Because, Silverlight 2 also can reference to Microsoft.Expression.Interactivity Assembly which is from Blend 3 library, you can take many advantages of Behaviors model from Silverlight 3 in Silverlight 2 as well. Yes, you can write Behaviors code without version dependency, and it can be referenced by both of Silverlight 2 and 3.

When I found that possibility, I was excited. As you know, Behaviors model brings you a great functionality which might reduce ‘bording-coding’ dramatically. All you have to do is that just copy-paste XAML from Blend 3, absolutely better than Blend 2, And even no code-behind!

I'm going to show you how to make a simple Silverlight 2 application using a simple trigger-action behavior. Create a new Silverlight project, and follow next steps.

Step 1. Referece a Microsoft.Expression.Interactivity Assembly.

Simply, copy a Microsoft.Expression.Interactivity Assembly to any folder to reference from your application. Usually, you can find it C:\Program Files\Microsoft Expression\Blend 3 Preview\Libraries\Silverlight folder if you installed Blend 3 Preview.

Notice that you must rename the assembly file, because Blend 2 can’t recongnize the name contains ‘Microsoft.Expression’. I don’t know why Blend 2 blocks specific assembly names like that. So just rename it say, Interactivy.dll or something. In this example, I renamed Microsoft.Expression.Interactivity.dll to Interactivity.dll. 

After you reference the assembly, you might be encountered an error follow:

It makes you blind in Visual Studio. Don’t panic. :) It’s  just a bug in Visual Studio Designer which already known. However Blend 2 will display it fine, if there was no other errors. Check this out further information.

Step 2. Create a simple trigger-action behavior.

Now, add a class say, ShowMessageBoxAction. It’ll show message box when trigger event raised on associated object.

public class ShowMessageBoxAction : TriggerAction< Dependencyobject>
{
    #region Message

    ///  
    /// Gets or sets the Message possible Value of the string object.
    ///  
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    ///  
    /// Identifies the Message dependency property.
    ///  
    public static readonly DependencyProperty MessageProperty =
    DependencyProperty.Register(
        "Message",
        typeof(string),
        typeof(ShowMessageBoxAction),
        new PropertyMetadata("Message")
    );

    #endregion Message

    protected override void Invoke(object parameter)
    {
        MessageBox.Show(Message);
    }
}

It has a property which you want to show up, and it ovverides Invoke method which is called when trigger event raised.

Step 3. Add a button with trigger.

Make some UI to check trigger performs well, follow:

<UserControl
    x:Class="SL2Behaviors.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:Microsoft.Expression.Interactivity;assembly=Microsoft.Expression.Interactivity"
    xmlns:local="clr-namespace:SL2Behaviors"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Click Me">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <local:ShowMessageBoxAction Message="Hello Behaviors in Silverlight 2!" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</UserControl>

Step 4. Build & run!

How is it? It works fine like Silverlight 3 does. Also you can use other Behaviors like this as well. Try it after these steps. Yes, now you can use Behaviors in your Silverlight 2 applications.

Here is source code for above. It'll be more helpful rather than my poor English. :)



Posted by gongdo

Submit comment.

  1. Favicon of http://www.timberlandbaratas.com BlogIcon Hombre Timberland 2012.12.24 12:57  comment URL  Edit/Remove  Submit comment.

    The airport security may also ask you to remove the laptop so that it can be dusted with a special device and tested for explosives, http://www.timberlandbaratas.com barato timberland.
    and if you don't intend to actually the laptop while on the plane, http://www.timberlandbaratas.com timberland niños,Traveling is not a Herculean task for you as you can enjoy the Cheap Flights through online way It is not possible for everyone to enjoy flights in current times as the airfares are touching the sky, http://www.timberlandbaratas.com zapatos timberland.You need to also remember the booking early will help you to snag a cheap seat, http://www.timberlandbaratas.com Hombre Timberland.
    Related articles:


    http://www.usbfax888.com/bbs/viewthread.php?tid=584745&extra= http://www.usbfax888.com/bbs/viewthread.php?tid=584745&extra=

    http://bbs.qs163.cn/thread-257241-1-1.html http://bbs.qs163.cn/thread-257241-1-1.html

    http://logosles.com/1185 http://logosles.com/1185

  2. Favicon of http://www.moncleroutletespain.com/ BlogIcon moncler online 2013.01.04 12:41  comment URL  Edit/Remove  Submit comment.

    L'authentification de la tête du roi Henri IV, http://www.moncleroutletespain.com/ moncler españa, annoncée par des scientifiques et des historiens à la mi-décembre, http://www.moncleroutletespain.com/ moncler outlet, est contestée par l'historien et journaliste Philippe Delorme, http://www.moncleroutletespain.com/ moncler online, dans un article publié mercredi par l'Express, http://www.moncleroutletespain.com/ moncler. Culture Paris a accueilli plus de 920 tournages en 2010 Culture Le cinéma fran, http://www.moncleroutletespain.com/ http://www.moncleroutletespain.com/?ais a connu une fréquentation record en 2010Related articles:


    http://jaystrange.tistory.com/624 http://jaystrange.tistory.com/624

    http://taeheeh.tistory.com/452?apppush http://taeheeh.tistory.com/452?apppush

  3. Favicon of http://www.moncleroutletespain.com/ BlogIcon moncler chaquetas 2013.01.04 15:02  comment URL  Edit/Remove  Submit comment.

    La maison de disques EMI, http://www.moncleroutletespain.com/ http://www.moncleroutletespain.com/, qui produit Pink Floyd depuis quarante ans, http://www.moncleroutletespain.com/ moncler españa, a conclu un nouvel accord de cinq ans avec le groupe britannique, http://www.moncleroutletespain.com/ moncler. Livres Biographie d'Hemingway : PPDA accusé de plagiat par l'Express Culture Paris a accueilli plus de 920 tournages en 2010Related articles:


    http://manmandi.tistory.com/692 http://manmandi.tistory.com/692

    http://nextgreendoor.tistory.com/177 http://nextgreendoor.tistory.com/177

  4. Favicon of http://www.moncleroutletespain.com/ BlogIcon moncler outlet 2013.01.04 17:37  comment URL  Edit/Remove  Submit comment.

    Le groupe pharmaceutique fran, http://www.moncleroutletespain.com/ moncler españa?ais Sanofi-Aventis et sa filiale BiPar Sciences ont annoncé jeudi la publication des résultats définitifs d'une étude de phase II présentant "des données positives" pour un "candidat-médicament" destiné au traitement d'un type de cancer du sein, http://www.moncleroutletespain.com/ moncler chaquetas. Santé L'étude qui liait l'autisme et le vaccin ROR était "un trucage" Actu en continu Bulgarie : un sanglier atteint de fièvre aphteuseRelated articles:


    http://charlieangus.tistory.com/26 http://charlieangus.tistory.com/26

    http://porce.net/?page=3 http://porce.net/?page=3

  5. Favicon of http://www.moncleroutletespain.com/ BlogIcon moncler españa 2013.01.05 21:20  comment URL  Edit/Remove  Submit comment.

    Au moins 22 personnes, donc six enfants, http://www.moncleroutletespain.com/ http://www.moncleroutletespain.com/, sont morts lorsque le bus qui les transportait a plongé dans un ravin, http://www.moncleroutletespain.com/ moncler, dans l'Etat de Uttarakhand (nord), a indiqué jeudi la police. L'accident, http://www.moncleroutletespain.com/ moncler chaquetas, qui s'est produit mercredi soir à une quinzaine de km de la capitale de l'Etat, http://www.moncleroutletespain.com/ moncler españa, Dehradun, a fait 19 blessés, http://www.moncleroutletespain.com/ moncler online, a précisé un responsable de la police à l'AFP, http://www.moncleroutletespain.com/ moncler outlet.Related articles:


    http://blue-paper.tistory.com/892 http://blue-paper.tistory.com/892

    http://arakis.blog.seoul.co.kr/?page=21 http://arakis.blog.seoul.co.kr/?page=21