Der Code:Warum RoutedCommand nicht an diesem Code arbeitet?
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");
}
}
Die XAML, es ist eine Testform, so gibt es nur, dass Button:
<Button x:Name="buttonTest" Width="30" Height="30">Test</Button>
Wenn ich die Schaltfläche "Test" klicken, passiert nichts, kein CanExecute, nein PreviewExecuted, no Executed ...
Was ist falsch an diesem Code?
Unglaublich! Ich dachte, dass buttonTest.CommandBindings.Add (testCommandBinding); war genug ... Danke! –