WPF事件从View到ViewModel的绑定? [英] WPF event binding from View to ViewModel?

查看:783
本文介绍了WPF事件从View到ViewModel的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我绑定在视图的视图模型?

一个WPF事件的最好办法

我在我的视图drop事件,但我想把它替换因绑定视图模型。
发现了几个解决方案,但没有人做了我aspected。

查看code:

 <文本框
    的AllowDrop =真
    previewDrop =email_Drop/>


解决方案

恕我直言,以处理MVVM和XAML事件的最好办法是使用混合交互功能。这个命名空间包含了InvokeCommandAction和CallMethodAction类。

InvokeCommandAction让你无论如何绑定到视图模型命令而CallMethodAction让你无论如何绑定到视图模型的方法。

例如,如果你想一个按钮到一个视图模型命令DoubleClick事件你会做这样的绑定:

 <按钮和GT;
    < I:Interaction.Triggers>
        < I:的EventTrigger事件名称=MouseDoubleClick>
            < I:InvokeCommandAction命令={绑定路径= DoSomethingCommand}/>
        < /我:&的EventTrigger GT;
    < /我:Interaction.Triggers>
< /按钮>

和声明这个命名空间:

 的xmlns:I =htt​​p://schemas.microsoft.com/ex$p$pssion/2010/interactivity

所有你需要引用它在你的项目是安装防爆pression混合或防爆pression混合SDK。

Whats the best way to bind a WPF event in the View to the ViewModel?

I got a drop event in my View but I want to replace it to the ViewModel due binding. Found several solutions but none of them did what I aspected.

View code:

    <TextBox 
    AllowDrop="True" 
    PreviewDrop="email_Drop" />

解决方案

IMHO, the best way to handle events in MVVM and XAML is to use the Blend Interactivity features. This namespace contains the InvokeCommandAction and the CallMethodAction classes.

InvokeCommandAction lets you bind any event to a view-model command while CallMethodAction lets you bind any event to a view-model method.

For example if you want to bind the DoubleClick event of a Button to a view-model command you would do like this:

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding Path=DoSomethingCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

And declaring this namespace:

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

All you need to reference it in your projects is to install Expression Blend or the Expression Blend SDK.

这篇关于WPF事件从View到ViewModel的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆