wpf eventsetter 处理程序绑定样式 [英] wpf eventsetter handler binding in style
问题描述
我有一个样式,我想用 RelativeSource
将命令绑定到 EventSetter
的 Handler
.该命令在 viewModel 中.
I have a style, and I want to bind a command to the EventSetter
's Handler
with RelativeSource
. The command is in the viewModel.
<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
<EventSetter Event="MouseLeftButtonDown"
Handler="{Binding TextBlockMouseLeftButtonDownCommand,
RelativeSource={RelativeSource Self}}"/>
</Style>
问题是我得到了一个错误,因为这有问题(也许不可能以如此简单的方式做到这一点)
The problem is that I get an error, because something is wrong with this (maybe it's not possible to do this in such easy way)
我之前在谷歌上搜索了很多,我找到了 AttachedCommandBehaviour
,但我认为它不适用于样式.
I've googled a lot before, and I found the AttachedCommandBehaviour
, but I think it doesn't work with style.
您能否就如何解决此问题提供一些提示?
Could you give some hints on how to solve this problem?
13/10/2011 更新
我在 MVVM Light Toolkit EventToCommand
示例程序中找到了这个:
I found this in the MVVM Light Toolkit EventToCommand
example program:
<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
放到按钮的样式中?
But here, the binding isn't in the style. How can I put this EventToCommand
to the style of the button?
推荐答案
现在您正在将 MouseLeftButtonDown
事件绑定到 TextBlock.TextBlockMouseLeftButtonDownCommand
.TextBlockMouseLeftButtonDownCommand
不是 TextBlock 的有效属性,听起来也不像是一个事件处理程序.
Right now you are binding the MouseLeftButtonDown
Event to TextBlock.TextBlockMouseLeftButtonDownCommand
. TextBlockMouseLeftButtonDownCommand
is not a valid property for a TextBlock, nor does it sound like it's an Event Handler.
我一直在样式中使用 AttachedCommandBehavior用于将命令连接到事件.语法通常如下所示(注意命令绑定中的 DataContext
):
I use the AttachedCommandBehavior all the time in styles for hooking up a Command to an Event. The syntax usually looks like this (note the DataContext
in the Command Binding):
<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
<Setter Property="local:CommandBehavior.Event" Value="MouseLeftButtonDown" />
<Setter Property="local:CommandBehavior.Command"
Value="{Binding DataContext.TextBlockMouseLeftButtonDownCommand,
RelativeSource={RelativeSource Self}}" />
</Style>
另一种方法是将 EventSetter 与代码隐藏中的事件挂钩,并从那里处理命令:
The alternative is to hook the EventSetter up to an event in the code-behind, and process the command from there:
<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
<EventSetter Event="MouseLeftButtonDown"
Handler="TextBlockMouseLeftButtonDown"/>
</Style>
隐藏代码中的事件处理程序...
Event handler in code behind...
void TextBlockMouseLeftButtonDown(object sender, MouseEventArgs e)
{
var tb = sender as TextBlock;
if (tb != null)
{
MyViewModel vm = tb.DataContext as MyViewModel;
if (vm != null && TextBlockMouseLeftButtonDownCommand != null
&& TextBlockMouseLeftButtonDownCommand.CanExecute(null))
{
vm.TextBlockMouseLeftButtonDownCommand.Execute(null)
}
}
}
这篇关于wpf eventsetter 处理程序绑定样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!