カスタム コマンドが実行されません。
XAML:
<Button Command="{Binding NewServer}" Content="Save" />
XAML のコード ビハインド:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new ServerViewModel();
}
}
サーバービューモデル:
public class ServerViewModel : DependencyObject {
public ICommand NewServer;
private readonly Dispatcher _currentDispatcher;
public ServerViewModel() {
NewServer = new NewServerCommand(this);
_currentDispatcher = Dispatcher.CurrentDispatcher;
}
public void SaveNewServers() {
throw new NotImplementedException("jhbvj");
}
}
新しいサーバー コマンド:
public class NewServerCommand : ICommand {
public event EventHandler CanExecuteChanged {
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
private readonly ServerViewModel _vm;
public NewServerCommand(ServerViewModel vm) {
_vm = vm;
}
public bool CanExecute(object parameter) {
Dispatcher _currentDispatcher = Dispatcher.CurrentDispatcher;
Action dispatchAction = () => MessageBox.Show("asd");
_currentDispatcher.BeginInvoke(dispatchAction);
return true;
}
public void Execute(object parameter) {
_vm.SaveNewServers();
}
}
CanExecute も Execute も呼び出されません。私は何を間違えましたか?