При смене свойства Button.CommandParameter у экземпляра Button.Command НЕ РЕЭВАЛУЭЙТИТСЯ CanExecute!
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Input;
5
6 class MainWindow: Window
7 { 8 class MyCommand: ICommand
9 { 10 private Window owner;
11
12 public MyCommand(Window owner)
13 { 14 this.owner = owner;
15 }
16
17 public event EventHandler CanExecuteChanged;
18
19 public void Execute(object parameter)
20 { 21 MessageBox.Show(owner, "Command Executed");
22 }
23
24 public bool CanExecute(object parameter)
25 { 26 return parameter != null;
27 }
28
29 public void update()
30 { 31 if(CanExecuteChanged!=null)
32 CanExecuteChanged(this,EventArgs.Empty);
33 }
34 }
35
36 private Button commandSource;
37 private MyCommand command;
38
39 public MainWindow()
40 { 41 StackPanel p = new StackPanel();
42 Content = p;
43
44 Button b = new Button();
45 b.Content = "Test";
46 b.Click += B_OnClick;
47 p.Children.Add(b);
48 b = new Button();
49 b.Content = "Command";
50 b.Command = command = new MyCommand(this);
51 p.Children.Add(b);
52 commandSource = b;
53 SizeToContent = SizeToContent.WidthAndHeight;
54 }
55
56 private void B_OnClick(object sender, RoutedEventArgs e)
57 { 58 commandSource.CommandParameter =
59 commandSource.CommandParameter == null ? this : null;
60
61 // Без этой строчки не работает
62 command.update();
63
64 Title =
65 (commandSource.CommandParameter == null).ToString();
66 }
67
68 [STAThread]
69 static void Main()
70 { 71 (new Application()).Run(new MainWindow());
72 }
73 }
Если закоментировать строку 62 то кнопка к которой привязана команда не будет блокироваться/разблокироваться несмотря на то что у кнопки меняется CommandParameter...
Интересно, баг или фича... Имхо баг...