środa, 20 listopada 2013

MVVM Command

 CS file

public class ViewModel : INotifyPropertyChanged
{
    // INotifyPropertyChanged implementation

    // Properties implementation

    public ViewModel()
    {
        ActionCommand = new MyCommand();
        ActionCommand.CanExecuteFunc = obj => true;
        ActionCommand.ExecuteFunc = MyActionFunc;
     }

     public void MyActionFunc(object parameter)
     {
         //Operations
     }
}

public class MyCommand : ICommand
{
    public Predicate<object> CanExecuteFunc{ get; set; }
    public Action<object> ExecuteFunc { get; set; }

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

    public event EventHandler CanExecuteChanged;
    public void Execute(object parameter)
    {
        ExecuteFunc(parameter);
    }
}

XAML file

 <Button Command="{Binding Path=ActionCommand}"
              CommandParameter="ParameterValue" />

Brak komentarzy:

Prześlij komentarz