WPF 通过事件调用命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:37:59  来源:igfitidea点击:

WPF calling commands via events

wpfeventscommand

提问by Trevor

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

是否可以通过 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

并且仍然以某种方式将对象作为参数传递。有没有一种方法可以在不使用代码的情况下实现这一点,因为我正在使用 MVVM 模式

Thanks for your time

谢谢你的时间

回答by Trevor

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.

为了后代,我添加了这个。

回答by Kent Boogaart

You can use attached behaviors to achieve this. Marlon Grechhas written the Attached Command Behaviorslibrary to save you the trouble. Usage looks like this:

您可以使用附加行为来实现这一点。Marlon Grech编写了附加命令行为库来为您省去麻烦。用法如下所示:

<Grid>
    <local:CommandBehaviorCollection.Behaviors>
        <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
    </local:CommandBehaviorCollection.Behaviors>
</Grid>

回答by Andy

I'm afraid I don't think what you want to do is possible. Commands aren't delegates, so you can't write a command up to an event. I think your best option is to handle the Button.LostFocusevent, and then manually execute the command from the handler.

恐怕我不认为你想做的事情是可能的。命令不是委托,所以你不能写一个命令直到一个事件。我认为您最好的选择是处理Button.LostFocus事件,然后从处理程序手动执行命令。

There is nothing wrong with putting code in the code behind when using MVVM, it is just best to minimize it and keep the code to view related tasks only. I would call this code view related, so it would be find to put the code in the code behind.

使用 MVVM 时将代码放在代码后面没有任何问题,最好将其最小化并保留代码仅用于查看相关任务。我会称此代码视图相关,因此会发现将代码放在后面的代码中。

回答by jongho

<Grid MouseRightButtonDown ="{eb:EventBinding Command=SomeCommand, CommandParameter=$e}">

</Grid>

Command

命令

{eb:EventBinding} (Simple naming pattern to find Command)

{eb:EventBinding Command=CommandName}

CommandParameter

命令参数

$e (EventAgrs) 

$this or $this.Property

string

https://github.com/JonghoL/EventBindingMarkup

https://github.com/JonghoL/EventBindingMarkup