如何将参数传递给事件触发器wpf中的方法

时间:2015-02-11 06:59:20

标签: c# wpf xaml

实际上,我试图在Xaml文件的ViewModel中存在的方法UpdateWord(object obj)中传递word文档的名称。这样它就会打开word文档。

<Button Content="Show Word" Width="100" Height="25" Margin="128,70,22,37">
    <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <si:CallDataMethod Method="UpdateWord"/>     
                <si:SetProperty TargetName="LayoutRoot" 
        PropertyName="Background" Value="PaleGoldenrod"/>
     </i:EventTrigger>
  </i:Interaction.Triggers>          

ViewModel:

public void UpdateWord(Object obj)
{

   //Do Something ..... ;
}

2 个答案:

答案 0 :(得分:2)

你可以这样做

  <i:EventTrigger EventName="Click">
        <cmd:EventToCommand Command="{Binding UpdateWord}"
            PassEventArgsToCommand="True" />
    </i:EventTrigger>

你可以参考这篇文章了解更多deatil:http://weblogs.asp.net/alexeyzakharov/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger

答案 1 :(得分:2)

有多种方法可以执行此操作,请查看here

  1. 使用WPF工具。最简单的
  2. 添加命名空间:

    • System.Windows.Interactivitiy
    • Microsoft.Expression.Interactions

    XAML:

    <Window>
        xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
    
        <wi:Interaction.Triggers>
            <wi:EventTrigger EventName="SelectionChanged">
                <ei:CallMethodAction
                    TargetObject="{Binding}"
                    MethodName="ShowCustomer"/>
            </wi:EventTrigger>
        </wi:Interaction.Triggers>
    </Window>
    

    代码:

    public void ShowCustomer()
    {
        // Do something.
    }
    
    1. 使用MVVMLight。最困难但最佳的做法
    2. 安装GalaSoft NuGet包。

      enter image description here

      获取名称空间:

      • System.Windows.Interactivity
      • GalaSoft.MvvmLight.Platform

      XAML

      <Window>
          xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
          xmlns:cmd="http://www.galasoft.ch/mvvmlight">
      
          <wi:Interaction.Triggers>
             <wi:EventTrigger EventName="Navigated">
                 <cmd:EventToCommand Command="{Binding NavigatedEvent}"
                     PassEventArgsToCommand="True" />
             </wi:EventTrigger>
          </wi:Interaction.Triggers>
      </Window>
      

      代表代码:Source

      你必须得到Prism MVVM NuGet包。

      enter image description here

      using Microsoft.Practices.Prism.Commands;
      
      // With params.
      public DelegateCommand<string> CommandOne { get; set; }
      // Without params.
      public DelegateCommand CommandTwo { get; set; }
      
      public MainWindow()
      {
          InitializeComponent();
      
          // Must initialize the DelegateCommands here.
          CommandOne = new DelegateCommand<string>(executeCommandOne);
          CommandTwo = new DelegateCommand(executeCommandTwo);
      }
      
      private void executeCommandOne(string param)
      {
          // Do something here.
      }
      
      private void executeCommandTwo()
      {
          // Do something here.
      }
      

      没有DelegateCommand的代码:Source

      using GalaSoft.MvvmLight.CommandWpf
      
      public MainWindow()
      {
          InitializeComponent();
      
          CommandOne = new RelayCommand<string>(executeCommandOne);
          CommandTwo = new RelayCommand(executeCommandTwo);
      }
      
      public RelayCommand<string> CommandOne { get; set; }
      
      public RelayCommand CommandTwo { get; set; }
      
      private void executeCommandOne(string param)
      {
          // Do something here.
      }
      
      private void executeCommandTwo()
      {
          // Do something here.
      }
      
      1. 使用Telerik EventToCommandBehavior。您必须下载NuGet Package。这是一个选择。
      2. XAML:

        <i:Interaction.Behaviors>
            <telerek:EventToCommandBehavior
                 Command="{Binding DropCommand}"
                 Event="Drop"
                 PassArguments="True" />
        </i:Interaction.Behaviors>
        

        代码:

        public ActionCommand<DragEventArgs> DropCommand { get; private set; }
        
        this.DropCommand = new ActionCommand<DragEventArgs>(OnDrop);
        
        private void OnDrop(DragEventArgs e)
        {
            // Do Something
        }