如何在传递事件参数的情况下将事件绑定到ViewModel上的方法.Silverlight 4

本文关键字:事件 ViewModel Silverlight 方法 绑定 情况下 参数 | 更新日期: 2023-09-27 17:57:49

我在某个地方看到,使用混合行为,这是100%可行的,找不到示例。

更好的例子是将事件args和/或sender作为CommandParameter传递给特定的命令。

如何在传递事件参数的情况下将事件绑定到ViewModel上的方法.Silverlight 4

   <i:Interaction.Triggers>
        <i:EventTrigger EventName="SizeChanged">
            <ei:CallMethodAction MethodName="WndSizeChanged"
                TargetObject="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

WndSizeChanged方法应该是公共的,并且具有与其订阅的事件委托相同的签名。

创建行为的一种简单方法是使用DelegateCommand方法,如Prism:

阅读此处:棱镜行为

Caliburn Micro有一些很好的方法可以做到这一点。

来自文档:

<Button Content="Remove"
        cal:Message.Attach="Remove($dataContext)" />
$eventArgs – Passes the Trigger’s EventArgs or input parameter to your Action. Note: This will be null for guard methods since the trigger hasn’t actually occurred.
$dataContext – Passes the DataContext of the element that the ActionMessage is attached to. This is very useful in Master/Detail scenarios where the ActionMessage may bubble to a parent VM but needs to carry with it the child instance to be acted upon.
$source – The actual FrameworkElement that triggered the ActionMessage to be sent.
$view - The view (usually a UserControl or Window) that is bound to the ViewModel.
$executionContext - The actions's execution context, which contains all the above information and more. This is useful in advanced scenarios.
$this - The actual ui element to which the action is attached.