Because Behaviors are not FrameworkElements, you can't use DataBinding with Behaviors. I posted this about it before.

<Button Content="Description">
        <i:EventTrigger EventName="Click">
            <local:ShowMessageBoxAction Message="{Binding Description}" />
[You can't use DataBinding with Behaviors like above]

Well, I'm still thinking about that it's the best way to use DataBinding with Behaviors. Why I can't do like that! However, if you really want to mix up DataBinding and Behaviors just like me, there are some work-arounds.

First, you can attach a 'Binder' class to FrameworkElement as an AttachedProperty. Morten Nielsen writed such a cool class, 'SurrogateBinder' which is general data binding helper. It could be attached to any FrameworkElement, and it passes through the value which from data source to actual target. Here's sample code:

<TextBox RenderTransformOrigin="0.5,0.5"
        Text="Hello Universe!"
        binders:SurrogateBind.Value="{Binding Path=MoreValues.Heading}" 
        binders:SurrogateBind.Target="RenderTransform.Children.Item[1].Angle" >
            <ScaleTransform />
            <RotateTransform />

It works well with any property of FrameworkElement but not for AttachedProperty like Behaviors. So you must modify little bit to targeting AttachedProperty. Also SurrogateBinder can't be set on Blend, so most of designers might not want to use it. Blend-support is important for XAML design. So I don't prefer in-line AttachedProperty as a solution, even though it is cool & smart.

SecondPete Blois who is a program manager of Expression Blend introduced this article to show how Binding property could be used with Blend for binding to non-FrameworkElement, like Behaviors. As you know, we can't use DataBinding to properties on Behaviors, 'cause Behavior is not a FrameworkElement. But Blend seems to allow DataBinding to a property which is type of Binding. Hey! it's so tricky isn't it? :(

    <id:DataStateBehavior Binding='{Binding IsOnline}' Value='True'/>
[You can use DataBinding only on a property which is type of Binding. Could you guess that?]

It looks much cooler than other, but it's not a general solution. Imagine that you want to bind to 3rd party's Behavior, you nerver can do, of course it might has no Binding property! However, sample is just sample and actually it's not only for DataBinding with Behavior but also for Trigger which is raised by condition of data.

Thrid. Now, here's my approach.

    <i:EventTrigger EventName="Click">
        <hi:ShowMessageBoxAction x:Name="ShowMessageAction"/>
        Binding="{Binding Path=Description}" />

Sigh, looks messy huh? But it based on standard Behavior, so it works well with Blend and you don't need to worry about writing these XAML code. Blend will generate XAML code as well as you write.
'BindingProxyBehavior' implements Behavior class, and it exposes  some properties which are needed to determine DataBinding & binding target. It focuses only one binding per an instance, but you can attach multiple instance of it, 'cause it's a standard Behavior.

Here's sample project. Enjoy it :)

Actually I'dont like any of them, but first code-block which is impossible currently. So I really want to say, why not with DataBinding? Please?
Posted by gongdo

Submit comment.

  1. Favicon of BlogIcon moncler 2013.01.05 21:20  comment URL  Edit/Remove  Submit comment.

    La Banque mondiale a annoncé samedi que son président Robert Zoellick effectuerait une visite de quatre jours en Inde à partir de lundi. Durant son séjour, moncler online, il rencontrera notamment le Premier ministre Manmohan Singh, le ministre des Finances Pranab Mukherjee et des responsables de la Commission du Plan, a indiqué l'institution internationale dans un communiqué, moncler españa. Son objectif est "d'avoir une meilleure compréhension de la fa, moncler chaquetas?on dont la Banque Mondiale pourrait aider l'Inde à remplir ses objectifs de développement à l'approche du 12e plan quinquennal", moncler, précise le communiqué, moncler outlet. Les discussions avec les responsables gouvernementaux porteront également sur les préparations des prochaines réunions du G20, a ajouté la Banque Mondiale, articles: