2015-02-11 63 views
1

其实我试图在Xaml文件的viewModel中存在的方法UpdateWord(object obj)中传递单词文档的名称。所以它会打开word文档。如何将参数传递给事件触发器中的方法wpf

<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>   

视图模型:

公共无效UpdateWord(obj对象) {

做点什么......;

}

回答

1

有这样做的多种方法,看here

  1. 使用WPF工具。最简单的

添加命名空间:

  • 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。最困难的,但最好的做法

安装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

你必须得到棱镜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. 
} 
  • 使用Telerik EventToCommandBehavior。你必须下载它的NuGet Package。这是一个选项。
  • 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 
    } 
    
    +0

    是这些股票的.NET?我认为他们是棱镜和telerik。你的第三个例子没有传递任何参数? – Julien 2015-04-11 00:23:10

    +0

    我很确定他们是Idk如何与Prism合作,我不知道telerik是什么。这些只是我发现堆栈溢出和其他网站的方式,我认为其他开发人员可能会觉得方便,我确实发现它很方便,所以我把它放在一个答案中。 – AzzamAziz 2015-04-11 00:32:58

    +0

    第三个传递绑定,无论它是从哪里继承。 – AzzamAziz 2015-04-11 00:33:14