How to pass an argument to a method present in trigger event wpf

In fact, I'm trying to pass the name of the word document in the UpdateWord (object obj) method present in the viewModel of the Xaml file. So he will open the word document.

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

}

+1
c # wpf xaml
Feb 11 '15 at 6:59
source share
2 answers

You can do it

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

you can refer to this post for more deatil: http://weblogs.asp.net/alexeyzakharov/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger

+1
Feb 11 '15 at 7:06
source share

There are several ways to do this, look here :

  • Using WPF tools. Simplest

Add namespaces:

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

the code:

 public void ShowCustomer() { // Do something. } 
  1. Using MVVMLight. The most difficult but effective practice.

Install the GalaSoft NuGet package.

enter image description here

Get namespaces:

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

Code with delegates: Source

To do this, you must get the Prism MVVM NuGet package.

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

Code without 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. Using Telerik EventToCommandBehavior . You will need to download its NuGet package . This is an option.

XAML:

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

the code:

 public ActionCommand<DragEventArgs> DropCommand { get; private set; } this.DropCommand = new ActionCommand<DragEventArgs>(OnDrop); private void OnDrop(DragEventArgs e) { // Do Something } 
+1
Feb 12 '15 at 4:19
source share



All Articles