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:
Submit comment.
제목이 너무 좋네요 실버라이트 하기 좋은날 (오늘 천둥번개 동반) ~ ㅎ
공도님 대단하세요 ^^
사실은 제목 줄임말이 별로 좋지 않아서 고민하다가 딱히 떠오르는게 없어서 이걸로 했어요.
부드러운 줄임말이 대센데 말이죠.
공실날? 공실좋? 실하날? 어떡게 줄여도 마음에 안드네요. 좋은 아이디어 있으면 주세요^^