2

Actually, I'm trying to pass the name of the word document in the method UpdateWord(object obj) present in the ViewModel of the Xaml file. So that it 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 ..... ;
}
Michael Armes
  • 1,056
  • 2
  • 17
  • 31
Yash Saxena
  • 43
  • 1
  • 4

2 Answers2

2

you can do like this

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

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

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

There are multiple ways of doing this, look here:

  1. Using WPF Tools. Easiest

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>

Code:

public void ShowCustomer()
{
    // Do something.
}
  1. Using MVVMLight. Most difficult but best practice

Install GalaSoft NuGet package.

enter image description here

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

You must get the Prism MVVM NuGet package for this.

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'll have to download it's NuGet Package. It's an option.

XAML:

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

Code:

public ActionCommand<DragEventArgs> DropCommand { get; private set; }

this.DropCommand = new ActionCommand<DragEventArgs>(OnDrop);

private void OnDrop(DragEventArgs e)
{
    // Do Something
}
Community
  • 1
  • 1
AzzamAziz
  • 2,144
  • 1
  • 24
  • 34
  • are any of these stock .net? i think they are prism and telerik. and your third example does not pass any arguments? – Julien Apr 11 '15 at 00:23
  • I'm pretty sure they are as Idk how to work with Prism and I have no idea what telerik is. These are just ways I found off of stack overflow and other sites that I thought other developers might find handy and I do find it handy myself so I put it all in one answer. – AzzamAziz Apr 11 '15 at 00:32
  • Third one passes binding, which is whomever it is inheriting from. – AzzamAziz Apr 11 '15 at 00:33
  • then maybe you can provide the namespaces for `ei` `wi` `beh` and even `i` because without them this is really hard to use – Julien Apr 11 '15 at 00:34
  • @Julien thank you for the comments. The code is now updated. I realized one of them was using MVVM Light, Prism is optional and Telerak was one of the examples as well. All namespaces and NuGet packages are added. – AzzamAziz Apr 15 '15 at 13:19