简体   繁体   English

从 View 到 ViewModel 的 WPF 事件绑定?

[英]WPF event binding from View to ViewModel?

What is the best way to bind a WPF event in the View to the ViewModel?将 View 中的 WPF 事件绑定到 ViewModel 的最佳方法是什么?

I have a drop event in my View but I want to replace it to the ViewModel due binding.我的视图中有一个放置事件,但我想将其替换为 ViewModel 到期绑定。

Found several solutions but none of them did what I expected.找到了几个解决方案,但没有一个达到我的预期。

View code:查看代码:

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

One way to handle events in MVVM and XAML is to use the Blend Interactivity features.在 MVVM 和 XAML 中处理事件的一种方法是使用混合交互功能。 This namespace contains the InvokeCommandAction and the CallMethodAction classes.此命名空间包含 InvokeCommandAction 和 CallMethodAction 类。

InvokeCommandAction lets you bind any event to a view-model command while CallMethodAction lets you bind any event to a view-model method. InvokeCommandAction 允许您将任何事件绑定到视图模型命令,而 CallMethodAction 允许您将任何事件绑定到视图模型方法。

For example if you want to bind the DoubleClick event of a Button to a view-model command you would do like this:例如,如果您想将 Button 的 DoubleClick 事件绑定到视图模型命令,您可以这样做:

<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.您需要在项目中引用它,只需安装 Expression Blend 或 Expression Blend SDK。

Well one way to do is to convert that event into a command and then bind it to presenter command, ie by defining event behaviour.一种方法是将该事件转换为命令,然后将其绑定到演示者命令,即通过定义事件行为。

See this, WPF Event Binding to ViewModel (for non-Command classes)看到这个, WPF Event Binding to ViewModel(对于非Command类)

<Button MouseDoubleClick="{eb:EventBinding Command=DoSomethingCommand}">

</Button>

Command命令

{eb:EventBinding} (Simple naming pattern to find Command) {eb:EventBinding}(查找命令的简单命名模式)

{eb:EventBinding Command=CommandName} {eb:EventBinding 命令=命令名称}

CommandParameter命令参数

$e (EventAgrs) $e (EventAgrs)

$this or $this.Property $this 或 $this.Property

string细绳

https://github.com/JonghoL/EventBindingMarkup https://github.com/JonghoL/EventBindingMarkup

I get the viewmodel from the bindingcontext and activate my viewmodel method from there我从 bindingcontext 获取 viewmodel 并从那里激活我的 viewmodel 方法

    public partial class ParentView : ContentPage
    {
            public ParentView()
            {            
                InitializeComponent();
            }

            private void LanguagePicker_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                var parentViewModel = (ParentViewModel)this.BindingContext;
                parentViewModel.SelectedLanguageChanged(sender,e);
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM