问题
I want to assign the same command to all my buttons on my user control. I use MVVM Light and I have tried all combinations. I have also tried to use EventSetter, but this does not allow binding to a command in the ViewModel
Here is a sample of what I am trying to do:<Style x:Key="CalculatorButton" TargetType="telerik:RadButton">
<Setter Property="Margin" Value="2"/>
<Setter Property="Cursor" Value="Hand"/>
<Style.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding ButtonClick}"/>
</i:EventTrigger>
</Style.Triggers>
</Style>
Design Time error: A value of type 'EventTrigger' cannot be added to a collection or dictionary of type 'TriggerCollection'.
Runtime Error: The value \"System.Windows.Interactivity.EventTrigger\" is not of type \"System.Windows.TriggerBase\" and cannot be used in this generic collection
回答1:
Unfortunately, an i:EventTrigger
is not the same thing as an EventTrigger
. The former is part of the System.Windows.Interactivity
and the latter is a core part of WPF. Since EventToCommand
uses System.Windows.Interactivity
, you'll have to use a different mechanism than Style.Triggers
to use it in a style. You can use my technique described in this StackOverflow answer:
- How to add a Blend Behavior in a Style Setter
来源:https://stackoverflow.com/questions/4883111/eventtocommand-in-button-style