【WPF】将控件事件中的参数,传递到ViewModel中

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

【WPF】将控件事件中的参数,传递到ViewModel中

干杯Archer   2021-04-25 我要评论

在MVVM模式下,在通常使用命令(Command)绑定的方式的时候 ,使用的是 CommandParameter 属性进行参数的传递。

但是很多时候,有一些事件我们需要使用其中的一些事件里面的参数,以获取相关数据或状态,但是使用命令绑定的方式又没办法达到这个要求,那么如何做呢?

 

1、引用相关命名空间

  

xmlns:Interaction="http://schemas.microsoft.com/expression/2010/interactions"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

 

2、在控件中使用命令

  

1 <控件>
2     <i:Interaction.Triggers>
3     <i:EventTrigger EventName="ExportEnd">
4       <Interaction:CallMethodAction TargetObject="{Binding}" MethodName="ExportEndCommand"/>
5       <!--<i:InvokeCommandAction Command="{Binding ExportEndCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self} }"/>-->
6     </i:EventTrigger>
7   </i:Interaction.Triggers>
8 </控件>

 

3、在 ViewModel中这样写

public void ExportEndCommand(object obj , Telerik.ReportViewer.Common.ExportEndEventArgs args)
        {

        }

 

根据自己的事件类型,进行参数的填写。

 

这样就可以在ViewModel中调用事件的参数了

  

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们