EventToCommand in button style

纵然是瞬间 提交于 2019-12-24 09:18:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!