这篇问题的解答说到了一个很容易被忽略的地方:
A CommandBinding
is just like any other element in your visual tree. Any events specified on it will be handled by the root of your visual tree (your Window
in this case).
所以必须在Code Behind里写点什么,才能让一个按钮的亮起与灰掉被判断。
Update:或者用实现了ICommand的东西来绑定到Command Property上(Button,MenuItem等都有),比如DelegateCommand。
之所以之前想直接绑定一个ICommand的集合到MenuItem的ItemsSource上不成功,是因为MenuItem生成的Sub MenuItem的DataContext是ICommand的时候,它自己不会写Command={Binding}, 所以Command并没有绑定成功。
少了下面语句干的活。
var menuItem = new MenuItem();
command.AttachRoutedUICommand(menuItem, MenuItem.CommandProperty, " ");
menuItem.CommandParameter = nodeData;