我已经看到使用混合行为100%可行,无法找到示例。
更好的例子是将事件args和/或发送者作为CommandParameter传递给特定命令。
答案 0 :(得分:4)
<i:Interaction.Triggers>
<i:EventTrigger EventName="SizeChanged">
<ei:CallMethodAction MethodName="WndSizeChanged"
TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
结束SizeChanged方法应该是公共的,并且与它订阅的事件委托具有相同的签名。
答案 1 :(得分:1)
创建行为的一种舒适方式是使用DelegateCommand方法,如Prism:
请在此处阅读:Prism behavior
答案 2 :(得分:1)
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.