我有一个样式,想要将一个命令与EventSetter
的Handler
绑定,并使用RelativeSource
。该命令在viewModel中。
<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
<EventSetter Event="MouseLeftButtonDown"
Handler="{Binding TextBlockMouseLeftButtonDownCommand,
RelativeSource={RelativeSource Self}}"/>
</Style>
问题是我遇到了错误,因为这个东西有问题(也许不能用这种简单的方式实现)。
之前我已经搜索过很多,发现了AttachedCommandBehaviour
,但我认为它不适用于样式。
你能否给一些提示来解决这个问题?
更新13/10/2011
我在MVVM Light Toolkit的EventToCommand
示例程序中找到了这个:
<Button Background="{Binding Brushes.Brush1}"
Margin="10"
Style="{StaticResource ButtonStyle}"
Content="Simple Command"
Grid.Row="1"
ToolTipService.ToolTip="Click to activate command">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding SimpleCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<cmd:EventToCommand Command="{Binding ResetCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
但是,在这里,绑定不在样式中。我该如何将EventToCommand
应用到按钮的样式中呢?