summaryrefslogtreecommitdiff
path: root/src/MVPtoMVVM.mvvm/viewmodels/SimpleCommand.cs
blob: 97e5b48ab90af989e286fcae5b13ec94c5814ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;

namespace MVPtoMVVM.mvvm.viewmodels
{
    public class SimpleCommand : IObservableCommand
    {
        private readonly Action command;
        private readonly Func<bool> predicate;
        public event EventHandler CanExecuteChanged = (o, e) => { };

        public SimpleCommand(Action command): this(command, () => true) {}

        public SimpleCommand(Action command, Func<bool> predicate)
        {
            this.command = command;
            this.predicate = predicate;
        }

        public void Execute(object parameter)
        {
            command();
        }

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

        public void Changed()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }

    }
}