Delegate Command Example in MVVM

Introduction

The Delegate Command can be used by implementing the System.Windows.Input.ICommand interface, the following bellow code for use of delegate command :

XAML Code

<Window x:Class="CommandMVVM.View.CharacterView"

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

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

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

mc:Ignorable="d"

d:DesignHeight="332" d:DesignWidth="477">

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width="Auto" />

      <ColumnDefinition Width="*" />

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0" Content="Comic character name:" Margin="5" />

    <Label Grid.Column="0" Grid.Row="1" Content="Secret identity:" Margin="5" />

    <Label Grid.Column="0" Grid.Row="2" Content="Good guy?" Margin="5" />

    <Label Grid.Column="0" Grid.Row="3" Content="Arch-enemy" Margin="5" />

    <Label Grid.Column="0" Grid.Row="4" Content="Powers" Margin="5" />

    <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Model.Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Model.SecretIdentity, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <CheckBox Grid.Column="1" Grid.Row="2" IsChecked="{Binding Model.Hero, UpdateSourceTrigger=PropertyChanged}" Margin="5,12,5,10"/>

    <ListBox Grid.Column="1" Grid.Row="3" Margin="5"

    ItemsSource="{Binding Model.Powers}">

      <ListBox.ItemTemplate>

        <DataTemplate>

          <Label Content="{Binding Description}" />

        </DataTemplate>

      </ListBox.ItemTemplate>

    </ListBox>

    <TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Model.ArchEnemy.Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <!--<Button Grid.Column="1" Grid.Row="5" Content="Save"

Width="65" Height="25" HorizontalAlignment="Right"

Command="{Binding SaveCommand}"/>-->

    <Button Grid.Column="1" Grid.Row="5" Content="{Binding SaveCommand2.Label}"

    Width="65" Height="25" HorizontalAlignment="Right"

    Command="{Binding SaveCommand2}"/>

  </Grid>

  </Window >

 
Add a new class in the ViewModes folder having following content

Code Behind

using System;

using System.Windows;

using CommandMVVM.Model;

using CommandMVVM.Framework;

 

namespace CommandMVVM.ViewModel

{

    public class ComicCharacterViewModel : ObjectBase

    {

        public ComicCharacterViewModel()

            : this(Character.Create())

        {

        }

        public ComicCharacterViewModel(Character model)

        {

            Model = model;

            SaveCommand = new SaveCharacterCommand(this);

            SaveCommand2 = new DelegateCommand<object>(SaveCommand_Execute, SaveCommand_CanExecute);

        }

        public Character Model { get; private set; }

 

        public SaveCharacterCommand SaveCommand { get; private set; }

 

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

 

        void SaveCommand_Execute(object arg)

        {

            // obviously this should not be done in the viewmodel

            MessageBox.Show(string.Format("{0} character saved.", Model.Name));

        }

        bool SaveCommand_CanExecute(object arg)

        {

            return Model.Name != string.Empty;

        }

    }

}

 
For binding the ComicCharacterViewModel class to windows class write the following code in to  the Initialized event 

ViewModel.ComicCharacterViewModel CCVM = new ViewModel.ComicCharacterViewModel();
            this.DataContext = CCVM;