Example of EventTrigger in MVVM Application

The following xaml Code is in View of mvvm

XAML CODE

<Window x:Class="Example04.View.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

xmlns:si="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

Title="MainWindow" Height="250" Width="300" MinHeight="200">

  <Grid>

    <Grid.RowDefinitions>

      <RowDefinition Height="110px" />

      <RowDefinition Height="*" />

    </Grid.RowDefinitions>

    <ListBox Name="employeeListBox" ItemsSource="{Binding empList}" Grid.Row="0" SelectedItem="{Binding SelectedIndex}">

      <i:Interaction.Triggers>

        <i:EventTrigger EventName="SelectionChanged">

          <i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=employeeListBox, Path=SelectedValue}"/>

        </i:EventTrigger>

      </i:Interaction.Triggers>

    </ListBox>

    <Grid Grid.Row="1" DataContext="{Binding SelectedEmployee}">

      <Grid.RowDefinitions>

        <RowDefinition Height="*" />

        <RowDefinition Height="*" />

        <RowDefinition Height="*" />

        <RowDefinition Height="*" />

      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>

        <ColumnDefinition Width="80" />

        <ColumnDefinition Width="*" />

      </Grid.ColumnDefinitions>

      <Label Grid.Row="0" Grid.Column="0">First Name</Label>

      <Label Grid.Row="1" Grid.Column="0">Last Name</Label>

      <Label Grid.Row="2" Grid.Column="0">Title</Label>

      <Label Grid.Row="3" Grid.Column="0">Department</Label>

 

 

      <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName, Mode=TwoWay}" />

      <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName, Mode=TwoWay}" />

      <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=Title, Mode=TwoWay}" />

      <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=Department, Mode=TwoWay}" />

    </Grid>

  </Grid>

</Window>

CodeBehind CODE

The folllwoing code is in viewModel of mvvm

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Example04.Model;

using Example04.Common;

using System.ComponentModel;

 

namespace Example04.ViewModel

{

    public class MainWindowViewModel : INotifyPropertyChanged

    {

        public List<Employee> empList { get; set; }

 

 

        private Employee selectedEmployee;

        public Employee SelectedEmployee

        {

            get { return selectedEmployee; }

            set

            {

                selectedEmployee = value;

                NotifyPropertyChanged("SelectedEmployee");

            }

        }

 

        public DelegateCommand<object> MyCommand { get; private set; }

 

        public MainWindowViewModel()

        {

            List<Employee> elist = new List<Employee>();

 

            elist.Add(new Employee() { EmployeeNumber = 1, FirstName = "John", LastName = "Dow", Title = "Accountant", Department = "Payroll" });

            elist.Add(new Employee() { EmployeeNumber = 2, FirstName = "Jane", LastName = "Austin", Title = "Account Executive", Department = "Employee Management" });

            elist.Add(new Employee() { EmployeeNumber = 3, FirstName = "Ralph", LastName = "Emmerson", Title = "QA Manager", Department = "Product Development" });

            elist.Add(new Employee() { EmployeeNumber = 4, FirstName = "Patrick", LastName = "Fitzgerald", Title = "QA Manager", Department = "Product Development" });

            elist.Add(new Employee() { EmployeeNumber = 5, FirstName = "Charles", LastName = "Dickens", Title = "QA Manager", Department = "Product Development" });

            empList = elist;

            MyCommand = new DelegateCommand<object>(Excute);

        }

 

        public void Excute(object employee)

        {

            SelectedEmployee = (Employee)employee;

        }

        protected void NotifyPropertyChanged(string propertyName)

        {

            if (PropertyChanged != null)

            {

                PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);

                this.PropertyChanged(this, args);

            }

        }

 

        public event PropertyChangedEventHandler PropertyChanged;

    }

}

The following code is in Model

CodeBehind
 

namespace Example04.Model

{

    public class Employee

    {

        public int EmployeeNumber { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Department { get; set; }

        public string Title { get; set; }

 

        public override string ToString()

        {

            return String.Format("{0} {1} ({2})", FirstName, LastName, EmployeeNumber);

        }

    }

}