Код:
public partial class MainWindow : Window
{
public static readonly RoutedCommand TestRoutedCommand = new RoutedCommand();
public MainWindow()
{
InitializeComponent();
CommandBinding testCommandBinding = new CommandBinding(TestRoutedCommand, Test_Executed, Test_CanExecute);
testCommandBinding.PreviewExecuted += Test_PreviewExecuted;
buttonTest.CommandBindings.Add(testCommandBinding);
// WHEN I UNCOMMENT THAT LINE, ONLY THE "Preview" MESSAGEBOX IS SHOWN
//TestRoutedCommand.Execute(null, buttonTest);
// Ok, I understood that part here: http://stackoverflow.com/a/4877259/48729
}
private void Test_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void Test_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Preview");
}
private void Test_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Executed");
}
}
XAML, это тестовая форма, поэтому есть только эта кнопка:
<Button x:Name="buttonTest" Width="30" Height="30">Test</Button>
Когда я нажимаю кнопку «Тест», ничего не происходит: нет CanExecute, нет PreviewExecuted, нет Executed...
Что не так с этим кодом?