使用InvokeCommandAction MVVM传递参数

时间:2015-07-16 13:39:02

标签: wpf mvvm devexpress-wpf

我想基于某些业务逻辑禁用单元格/列。我使用ShowingEditor事件和ShowingEditorEventArgs取消它。传递ShowingEditorEventArgs将是非常好的。我能够将整个网格作为参数传递。使用下面的代码。但我只想通过所选单元格的ShowingEditorEventArgs。可能是一些相对资源绑定帮助我在这里。

<dxg:GridControl x:Name="grid" >
                <dxg:GridControl.View>
                    <dxg:TableView Name="view"  ShowingEditor="view_ShowingEditor">
                    <i:Interaction.Triggers>

                        <i:EventTrigger EventName="ShowingEditor">
                            <i:InvokeCommandAction Command="{Binding ShowingEditorCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type dxg:GridControl}}}" />

... 注意:

  1. 我无法使用 MVVM指示灯 GalaSoft )。
  2. 互动不会给我CallMethodAction。

    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    <ei:CallMethodAction
    
  3. 我不想传递ViewModel的绑定属性(例如SelectedItem

  4. 使用 DevExpress GridControl

1 个答案:

答案 0 :(得分:2)

考虑使用DevExpress MVVM Framework,您可以将事件参数作为参数传递给视图模型:

<dxmvvm:Interaction.Behaviors>
    <dxmvvm:EventToCommand EventName="ShowingEditor" Command="{Binding ShowingEditorCommand}" PassEventArgsToCommand="True" />
</dxmvvm:Interaction.Behaviors>

甚至可以在使用EventArgsConverter属性指定的转换器将EventArgs对象传递给命令之前对其进行转换。

查看EventToCommand文章了解详情。

P.S。如果由于某种原因无法使用DevExpress MVVM Framework,this post描述了如何实现自定义TriggerAction将事件args手动传递给命令。