10 public partial class Window1 : Window
11 {
12 public ObservableCollection<Person>
13 Persons { get; set; }
14
15 public DelegateCommand<Person>
16 RetirementCommand { get; set; }
17
18 public Window1()
19 {
20 Persons = new ObservableCollection<Person>();
21 Persons.Add(new Person() {
22 Name = "OleBob", Age = 73 });
23 Persons.Add(new Person() {
24 Name = "OleLou", Age = 82 });
25 Persons.Add(new Person() {
26 Name = "LilJohn", Age = 20 });
27 Persons.Add(new Person() {
28 Name = "LilJames", Age = 32 });
29
30 InitializeComponent();
31
32 RetirementCommand =
33 new DelegateCommand<Person>(Execute, CanExecute);
34 DataContext = this;
35
36 lstPersons.SelectionChanged += delegate {
37 cmMenuItem.CommandParameter =
38 lstPersons.SelectedValue;
39
40 RetirementCommand.RaiseCanExecuteChanged();
41 };
42
43 }
44
45 public void Execute(Person p)
46 {
47 MessageBox.Show(((Person)p).Name);
48 }
49
50 public bool CanExecute(object p)
51 {
52 if ((p != null) && (p is Person))
53 return ((Person)p).Age >= 65;
54
55 return false;
56 }
57 }
58
59 public class Person
60 {
61 public string Name { get; set; }
62 public int Age { get; set; }
63 }
64 }
public Window1()
{
Persons = new ObservableCollection<Person>();
Persons.Add(new Person() {
Name = "OleBob", Age = 73 });
Persons.Add(new Person() {
Name = "OleLou", Age = 82 });
Persons.Add(new Person() {
Name = "LilJohn", Age = 20 });
Persons.Add(new Person() {
Name = "LilJames", Age = 32 });
InitializeComponent();
RetirementCommand =
new DelegateCommand<Person>(Execute, CanExecute);
DataContext = this;
lstPersons.SelectionChanged += delegate {
cmMenuItem.CommandParameter =
lstPersons.SelectedValue;
RetirementCommand.RaiseCanExecuteChanged();
};