WPF通过事件调用命令 [英] WPF calling commands via events

查看:664
本文介绍了WPF通过事件调用命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过WPF中的事件调用命令?



我有一个保存按钮,当按下时调用命令,当您完成编辑文本框,它还将一个对象作为命令参数传递

 &Button Button =SaveCommand ={Binding DataContext.SaveQueueTimeCommand,RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ItemsControl}}}CommandParameter ={Binding}/> 

我最理想的做法是调用这个命令,并传递对象作为参数,当文本框失去焦点,而不是必须按下按钮:

 < Button LostFocus ={Binding SaveQueueTimeCommand}/ > 

仍然以某种方式将对象作为参数传递。
有一种方法可以不使用代码背后,因为我使用MVVM模式



感谢您的时间

 <$ c $ 

c>< Grid xmlns:i =http://schemas.microsoft.com/expression/2010/interactivity>
< i:Interaction.Triggers>
< i:EventTrigger EventName =SomeEvent>
< i:InvokeCommandAction Command ={Binding Path = SomeCommand,Mode = OneWay}/>
< / i:EventTrigger>
< / i:Interaction.Triggers>
< / Grid>

我为了后代添加了这个。


Is it possible to call a command via an event in WPF?

I have a save button that when pressed calls a command, this is pressed when you have finished editing a textbox, it also passes an object as a command parameter

 <Button Content="Save"  Command="{Binding DataContext.SaveQueueTimeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}" />

What I would ideally like to do is call this command and pass the object as a parameter when the textbox loses focus, rather than having to press the button, something like:

 <Button LostFocus="{Binding SaveQueueTimeCommand}" />

And still somehow pass the object as a parameter. Is there a way to acheive this without using code behind as I am using the MVVM pattern

Thanks for your time

解决方案

The simplest way to do this is using an interaction trigger.

<Grid xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SomeEvent">
            <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Grid>

I have added this for posterity sake.

这篇关于WPF通过事件调用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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