forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModelCommand.cs
More file actions
97 lines (82 loc) · 2.57 KB
/
Copy pathViewModelCommand.cs
File metadata and controls
97 lines (82 loc) · 2.57 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace PowerShellTools.Explorer
{
internal class ViewModelCommand : ICommand
{
private readonly ViewModel _vm;
private readonly Action _execute = null;
public ViewModelCommand(ViewModel vm, Action execute)
{
_vm = vm;
_vm.PropertyChanged += new PropertyChangedEventHandler(OnViewModelPropertyChanged);
_execute = execute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (_execute != null)
{
_execute();
}
}
public bool CanExecute(object parameter)
{
return true;
}
private void RaiseCanExecuteChanged()
{
EventHandler h = CanExecuteChanged;
if (h != null)
{
h(this, new EventArgs());
}
}
private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.RaiseCanExecuteChanged();
}
}
internal class ViewModelCommand<T> : ICommand
{
private readonly ViewModel _vm;
private readonly Action<T> _execute = null;
private readonly Predicate<T> _canExecute = null;
public ViewModelCommand(ViewModel vm, Action<T> execute)
:this(vm, execute, null)
{
}
public ViewModelCommand(ViewModel vm, Action<T> execute, Predicate<T> canExecute)
{
_vm = vm;
_vm.PropertyChanged += new PropertyChangedEventHandler(OnViewModelPropertyChanged);
_execute = execute;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute((T)parameter) : true;
}
public void Execute(object parameter)
{
if (_execute != null)
{
_execute((T)parameter);
}
}
private void RaiseCanExecuteChanged()
{
EventHandler h = CanExecuteChanged;
if (h != null)
{
h(this, new EventArgs());
}
}
private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.RaiseCanExecuteChanged();
}
}
}