프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Mvvm.ToolKit / RelayCommand / RelayCommand.cs @ 45f9a2ad

이력 | 보기 | 이력해설 | 다운로드 (2.73 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics.CodeAnalysis;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows.Input;
8

    
9
namespace Markus.Mvvm.ToolKit
10
{
11
    public class RelayCommand<T> : ICommand
12
    {
13
        private readonly Predicate<T> _canExecute;
14
        private readonly Action<T> _execute;
15

    
16
        public RelayCommand(Action<T> execute)
17
           : this(execute, null)
18
        {
19
            _execute = execute;
20
        }
21

    
22
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
23
        {
24
            if (execute == null)
25
            {
26
                throw new ArgumentNullException("execute");
27
            }
28
            _execute = execute;
29
            _canExecute = canExecute;
30
        }
31

    
32
        public bool CanExecute(object parameter)
33
        {
34
            return _canExecute == null || _canExecute((T)parameter);
35
        }
36

    
37
        public void Execute(object parameter)
38
        {
39
            _execute((T)parameter);
40
        }
41

    
42
        public event EventHandler CanExecuteChanged
43
        {
44
            add { CommandManager.RequerySuggested += value; }
45
            remove { CommandManager.RequerySuggested -= value; }
46
        }
47
    }
48

    
49
    public class RelayCommand : ICommand
50
    {
51
        private readonly Predicate<object> _canExecute;
52
        private readonly Action<object> _execute;
53

    
54
        public RelayCommand(Action<object> execute)
55
           : this(execute, null)
56
        {
57
            _execute = execute;
58
        }
59

    
60
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
61
        {
62
            if (execute == null)
63
            {
64
                throw new ArgumentNullException("execute");
65
            }
66
            _execute = execute;
67
            _canExecute = canExecute;
68
        }
69

    
70
        public bool CanExecute(object parameter)
71
        {
72
            return _canExecute == null || _canExecute(parameter);
73
        }
74

    
75
        public void Execute(object parameter)
76
        {
77
            _execute(parameter);
78
        }
79

    
80
        // Ensures WPF commanding infrastructure asks all RelayCommand objects whether their
81
        // associated views should be enabled whenever a command is invoked 
82
        public event EventHandler CanExecuteChanged
83
        {
84
            add
85
            {
86
                CommandManager.RequerySuggested += value;
87
                CanExecuteChangedInternal += value;
88
            }
89
            remove
90
            {
91
                CommandManager.RequerySuggested -= value;
92
                CanExecuteChangedInternal -= value;
93
            }
94
        }
95

    
96
        private event EventHandler CanExecuteChangedInternal;
97

    
98
        public void RaiseCanExecuteChanged()
99
        {
100
            CanExecuteChangedInternal.Raise(this);
101
        }
102
    }
103
}
클립보드 이미지 추가 (최대 크기: 500 MB)