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

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


티스토리 툴바