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)

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:EventTrigger EventName="Click">
            <local:ShowMessageBoxAction Message="{Binding Description}" />

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.