1 Results for 'ask Microsoft'

  1. 2009.06.11 Behaviors make me depressed. (1)
I posted how to use DataBinding with Behaviors in Silverlight 3 recently. However, it turns out, it's impossible or very limitted.

Follow code is what I expected to work.

<Grid x:Name="LayoutRoot" Background="White">
    <Button Content="Test" HorizontalAlignment="Right">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <hi:ShowMessageBoxAction x:Name="Action1"/>
            </i:EventTrigger>
        </i:Interaction.Triggers

        <i:Interaction.Behaviors>
            <hi:BindingProxyBehavior
                        TargetName="Action1"
                        TargetPropertyName="Message"
                        Binding="{Binding Path=MockString}" />
        </i:Interaction.Behaviors>
    </Button>
</Grid>

[It doesn't work, 'cause Behaviors are not FrameworkElements]

- ShowMessageBoxAction which named "Action1" by x:Name attribute has a property named Message.
- BindingProxyBehavior is a Behavior. And it figures out "Action1" by AssociatedObject(LayoutRoot).FindName method when it has been invoked.

But It's impossible, 'cause Behaviors are not Frameworks so they can't be found by FrameworkElement.FindName method.

It's OK, I can understand why is it impossible. However how do you think about code follow:

<DataTemplate x:Key="DataTemplate1">
    <Grid>
        <Button HorizontalAlignment="Left" Content="{Binding Mode=OneWay, Path=Title}">
            <i:Interaction.Triggers>
                <i:EventTrigger x:Name="Trriger1" EventName="Click">
                    <hi:ShowMessageBoxAction x:Name="ShowMessageAction"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>           

            <i:Interaction.Behaviors>
                <hi:BindingProxyBehavior
                    TargetName="ShowMessageAction"
                    TargetPropertyName="Message"
                    Binding="{Binding Path=Description}" />
            </i:Interaction.Behaviors>
        </Button>
    </Grid>
</DataTemplate>

[Does it work or not?]

It's similar to first one, except root Grid is in the DataTemplate.
Guess what? it works. Very well.
HOW COME!? Isn't it wierd?

Now I have to give up to do DataBinding with Behaviors in Silverlight 3 by that way.
It makes me depressed. "No binding, more work".
Anyway all I really want to ask Microsoft is, just allow us DataBinding with Behaviors, so we can create application rapidly, design-friendly, and even elegantly! Like follow code:

<Grid x:Name="LayoutRoot">
    <Button Content="Test">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <hi:ShowMessageBoxAction
                    x:Name="Action1"
                    Message="{Binding Path=Description}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</Grid>

[Elegant DataBinding with Behaviors]

Here for sample project:

저작자 표시 동일 조건 변경 허락
신고
Posted by gongdo


티스토리 툴바