Rediscovering the Obvious

…stumbling in the footsteps of greatness

Is this a good thing?

without comments

My understanding of the .NET design philosophy is that if you handle an event with an empty handler, it should behave essentially as if you had not handled the event at all.  This may not be the case, but it seems to be in most things… if you handle the Close window event and don’t do anything, the window still closes.

However, with WPF, this isn’t always the case. For example, the following line of code adds a pair of new handlers to a defined command:

    Application.Current.MainWindow.CommandBindings.Add(
        new CommandBinding(
            Commands.ToggleVisibility,
            new ExecutedRoutedEventHandler( ToggleCommand ),
            new CanExecuteRoutedEventHandler( CanToggleCommand )));

If you implement an empty handler for ToggleCommand, it gets called and life goes on.
If you implement an empty handler for CanToggleCommand, everything grinds to a halt because the COMMAND IS NO LONGER CALLED.

Why is this? Why do I have have to explicitly set args.CanExecute = true? Why does an empty handler change the behavior?

</rant>
 

 

Written by erwilleke

June 8th, 2007 at 8:38 am

Posted in Uncategorized

Leave a Reply