【WPF】將控件事件中的參數,傳遞到ViewModel中


在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中調用事件的參數了

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM