locked
Command and disable MenuItem RRS feed

  • Question

  • Can a Command manipulate the Enable Property of a UserControl.

    For example disable a MenuItem if it's command can't execute?

     

    Friday, December 1, 2006 8:54 AM

All replies

  • The usual way to do this is with command bindings that specify a handler for CanExecute on the same element that handles the command.

    <Window.CommandBindings>
     
    <
    CommandBinding Command="ApplicationCommands.Undo"
       
    Executed="Command_Undo"
        CanExecute="Command_CanUndo"/>

    ...

    </Window.CommandBindings>

    private void Command_CanUndo(object sender, CanExecuteRoutedEventArgs args)
    {
       args.CanExecute = undoEngine.CanUndo;
       args.Handled =
    true;
    }

    This will selectively enable and disable any menu item for which Command='ApplicationCommands.Undo' .


    (irrelevant note: In actual code this command binding used a PreviewExecuted handler in order to intercept standard unod handling for edit controls).

     

    Friday, December 1, 2006 2:07 PM