Contristo
Contristo

Reputation: 335

Problems with custom WPF Commands using MVVM

I have tried two different methods of commanding in WPF from the ViewModel and come up short. The top method works great if I put it into the View however I was told this is bad practice. The second method I was told is the proper way to do custom commanding in MVVM however I get stuck on how to actually call/bind the command from the View.

View Model:

class MainViewModel : INotifyPropertyChanged
{

    #region INPC
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion


     public readonly static RoutedUICommand myCommand;

    static MainViewModel()
    { 
        myCommand = new RoutedUICommand("customCommand","My Command",typeof(MainViewModel));

    }

    private void ExecutemyCommand(object sender, ExecutedRoutedEventArgs e)
    {

        MessageBox.Show("textBox1 cleared");
    }

    private void myCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}

On my View the code I have this which gives me an error

<Window x:Class="ConfigManager2.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:ConfigManager2.Converters"
    xmlns:vm="clr-namespace:ConfigManager2.ViewModel"
    xmlns:local="clr-namespace:ConfigManager2.View"
.
.
.
<Window.CommandBindings>
    <CommandBinding
        Command="{x:Static vm:MainViewModel.myCommand}"
        CanExecute="myCommandCanExecute"
        Executed="ExecutemyCommand" />
</Window.CommandBindings>
.
.
.
 <Button Content="COMMAND ME" Height="50px"  Command="{x:Static vm:MainViewModel.myCommand}" />

The Error I am getting is 'ConfigManager2.View.MainView' does not contain a definition for 'ExecutemyCommand' and no extension method 'ExecutemyCommand' accepting a first argument of type 'ConfigManager2.View.MainView' could be found (are you missing a using directive or an assembly reference?)

I tried another method using ICommand and got stumped on how to bind this to the above button from the XAML

ViewModel:

    public ICommand ClearCommand { get; private set; }
public MainViewModel()
{
    ClearCommand= new ClearCommand(this);
}



class ClearCommand : ICommand
{
    private MainViewModel viewModel;

    public ClearCommand(MainViewModel viewModel)
    {
        this.viewModel = viewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        viewModel.vmTextBox1 = String.Empty;
        MessageBox.Show("Textbox1 Cleared");
    }
}

Upvotes: 0

Views: 854

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32637

With the ICommand version (which I would prefer) you can bind directly to the command:

<Button Command="{Binding ClearCommand}"/>

There is no Window.CommandBinding necessary. This works, if an instance of the MainViewModel is set as the window's or button's DataContext.

Upvotes: 1

Related Questions