wpf 事件处理程序绑定样式
我有一个样式,我想使用 RelativeSource
将命令绑定到 EventSetter
的 Handler
。该命令位于 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
放入按钮的样式?
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)
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?
Update 13/10/2011
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>
But here, the binding isn't in the style. How can I put this EventToCommand
to the style of the button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在,您正在将
MouseLeftButtonDown
事件绑定到TextBlock.TextBlockMouseLeftButtonDownCommand
。TextBlockMouseLeftButtonDownCommand
不是 TextBlock 的有效属性,听起来也不像是一个事件处理程序。我一直使用 AttachedCommandBehavior 样式进行连接对事件的命令。语法通常如下所示(请注意命令绑定中的 DataContext):
另一种方法是将 EventSetter 挂接到代码隐藏中的事件,并从那里处理命令:
事件处理程序代码后面...
Right now you are binding the
MouseLeftButtonDown
Event toTextBlock.TextBlockMouseLeftButtonDownCommand
.TextBlockMouseLeftButtonDownCommand
is not a valid property for a TextBlock, nor does it sound like it's an Event Handler.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):The alternative is to hook the EventSetter up to an event in the code-behind, and process the command from there:
Event handler in code behind...
由于您正在使用 MVVM,我建议您 Galasoft MVVM Light Toolkit
EventToCommand
As you are using MVVM, I suggest you Galasoft MVVM Light Toolkit
EventToCommand
我对这个问题的回答< /a> 无需任何外部工具包/库即可完成此操作。但是,它不使用
RelativeSource
,并且它不是 100% MVVM。它需要代码隐藏事件处理程序中的一行代码。My answer on this question does the trick without any external tool kits/libraries. However, it does not use
RelativeSource
, and it is not 100% MVVM. It requires one line of code in a code-behind event handler.