MVVM模式下,View触发任意事件在ViewModel中响应

   由于使用的是prism框架,所有需要首先引入Prism4\Lib\Desktop下的

Microsoft.Expression.Interactions.dll

System.Windows.Interactivity.dll

XAML文件

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

绑定事件

<Button Content="MouseLeave事?件t" Width="70" Height="25">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="MouseLeave"  >

                    <i:InvokeCommandAction Command="{Binding MouseLeave}"

                                           CommandParameter="123"/>

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </Button>

ViewModel文件

     public ICommand MouseLeave

        {

            get

            {

                return new DelegateCommand<string>(x =>

                {

                    MessageBox.Show("MouseLeave" + x);

                });

            }

        }

注意事项:

Microsoft.Expression.Interactions.dll

System.Windows.Interactivity.dll

prismC:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF都有,并且WPFSilverligter都有这些文件(不能通用),如果引用的是blend中的dll,则需要通过xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"方式引用,但是绑定事件的方式和从prism引用的dll不同,这些问题纠结了我半天,所以建议dllprim文件夹下引用

Dllhttp://compositewpf.codeplex.com/下载

MVVM模式下,View触发任意事件在ViewModel中响应

原文地址:https://www.cnblogs.com/gossip/p/2096699.html