WPF MVVM 事件到命令 - 基于事件参数调用命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15411993/
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
WPF MVVM event to command - invoke command based on event args
提问by rinat
I have a WPF app which uses DevExpress controls and MVVM with PRISM.
I'm using DockLayoutManager's 'DockOperationCompleted' event to invoke a command on my view model like this:
我有一个 WPF 应用程序,它使用 DevExpress 控件和带有 PRISM 的 MVVM。
我正在使用 DockLayoutManager 的 'DockOperationCompleted' 事件来调用我的视图模型上的命令,如下所示:
<dxd:DockLayoutManager x:Name="dockContainer">
<i:Interaction.Triggers>
<i:EventTrigger EventName="DockOperationCompleted">
<i:InvokeCommandAction Command="{Binding DataContext.SaveLayoutCommand, ElementName=dockContainer}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<dxd:LayoutGroup/>
</dxd:DockLayoutManager>
The purpose of the 'SaveLayoutCommand' command is to save the layout so it can be restored later in time.
The 'DockOperationCompleted' event is raised after a DockItem gets docked or closed (there are other cases but they are irrelevant).
The problem is that when I close the main window, the dock items in my DockLayoutManager are getting closed one by one and thus 'SaveLayoutCommand' gets invoked for every closed dock item and I don't want this to happen.
The 'DockOperationCompletedEventArgs' with which the event gets raised has a 'DockOperation' property which I can check agains, but I'm not sure where exactly should this code fit in.
What I am trying to achieve is that the command should be invoked only in one case - when the item is docked
My question is: is there a way to 'filter' when the command gets invoked based on the event's event args?
“SaveLayoutCommand”命令的目的是保存布局,以便稍后可以恢复。
'DockOperationCompleted' 事件在 DockItem 停靠或关闭后引发(还有其他情况,但它们无关紧要)。
问题是当我关闭主窗口时,我的 DockLayoutManager 中的停靠项会一个一个关闭,因此每个关闭的停靠项都会调用“SaveLayoutCommand”,我不希望这种情况发生。
引发事件的“DockOperationCompletedEventArgs”有一个“DockOperation”属性,我可以再次检查,但我不确定这段代码到底应该放在什么地方。
:当基于事件的事件参数调用命令时,有没有办法“过滤”?
Thanks:)
谢谢:)
回答by tziemek
In addition to aKzenTs answer I want to point out that with DevExpress its rather easy to pass the event args to a command.
除了 aKzenTs 答案之外,我想指出,使用 DevExpress 将事件参数传递给命令相当容易。
You should use EventToCommand from their MVVM library (PassEventArgsToCommand-Property). If you want to keep your viewmodel clean of DevExpress you can additionally use a Converter to transform the event args to an arbitrary object.
您应该使用他们的 MVVM 库 (PassEventArgsToCommand-Property) 中的 EventToCommand。如果你想让你的视图模型保持 DevExpress 干净,你可以另外使用转换器将事件参数转换为任意对象。
<dxmvvm:Interaction.Triggers>
<dxmvvm:EventToCommand Command="{Binding YOURCOMMAND}"
EventName="THEEVENT"
EventArgsConverter="{StaticResource YOUREVENTARGSCONVERTER}"
PassEventArgsToCommand="true" />
</dxmvvm:Interaction.Triggers>
回答by aKzenT
There is no builtin way to filter the events that are raised before invoking an action. You can however implement your own custom trigger action that does the filtering.
没有内置方法来过滤在调用操作之前引发的事件。但是,您可以实现自己的自定义触发器操作来进行过滤。
Unfortunately it's also not easy to access the event args and passing them to the command. See this question as a reference:
不幸的是,访问事件参数并将它们传递给命令也不容易。请参阅此问题作为参考:

