using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp55.ViewModel
{
public class VM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
private DelegateCommand UCCmdValue;
public DelegateCommand UCCmd
{
get
{
if(UCCmdValue==null)
{
UCCmdValue = new DelegateCommand(UCCmdExecuted, UCCmdCanExecute);
}
return UCCmdValue;
}
}
private bool UCCmdCanExecute(object obj)
{
return true;
}
private void UCCmdExecuted(object obj)
{
MessageBox.Show("You had clicked the customized button!");
}
}
}
1 public class DelegateCommand : ICommand
2 {
3 private readonly Predicate<object> _canExecute;
4 private readonly Action<object> _execute;
5
6 public event EventHandler CanExecuteChanged;
7
8 public DelegateCommand(Action<object> execute)
9 : this(execute, null)
10 {
11 }
12
13 public DelegateCommand(Action<object> execute,
14 Predicate<object> canExecute)
15 {
16 _execute = execute;
17 _canExecute = canExecute;
18 }
19
20 public bool CanExecute(object parameter)
21 {
22 if (_canExecute == null)
23 {
24 return true;
25 }
26
27 return _canExecute(parameter);
28 }
29
30 public void Execute(object parameter)
31 {
32 _execute(parameter);
33 }
34
35 public void RaiseCanExecuteChanged()
36 {
37 if (CanExecuteChanged != null)
38 {
39 CanExecuteChanged(this, EventArgs.Empty);
40 }
41 }
42 }