locked
RoutedCommand RRS feed

  • Question

  • Hello, I'm tryin' to make a custom RoutedCommand.

    Ok, my cuestion is about MenuItem in ContextMenu control. In a TreeView, I have more different types of nodes, and in every one, I can create a "new" node of each type, so I have a codebehind method

     

    Code Snippet

    void AddItem(Type type)

     

    Now, I have a Click event declared in every MenuItem in XAML, but I guess it will be more usefull to make a command, but I don't know how.

    I think, if I can Command each MenuItem from the ContextMenu, and only changin' CommanParameter with the type that I want, it will be my solution. I mean, something like:

    Code Snippet

    <ContextMenu ... Command="{AddItem}"

    <MenuItem Header="Add node type1" CommandParameter="{type1}"

    <MenuItem Header="Add node type2" CommandParameter="{type2}"

    ...............................................................................

    <MenuItem Header="Add node typeN" CommandParameter="{typeN}"

     

     

    And in codebehind threat the parameter-value passed to the method.

    How can I do that?? Is it possible??

     

    Thanks.

    Tuesday, April 3, 2007 1:42 PM

Answers

  • The example given to you has everything needed for you to create a command, add a command binding, and have your code behind you declared in your binding called.

     

    However you'll have to redefine this command on each MenuItem, together with the CommandParameter. You'll receive it in your handler. In the case of the example offered on the link we were talking about:

     

     

    Code Snippet
    public void ExecutedCustomCommand(object sender,
        ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Custom Command Executed with param: " + e.Parameter.ToString());
    }
    

     

    Wednesday, April 4, 2007 10:23 AM

All replies

  • Please take a look at How to: Create a RoutedCommand.
    Wednesday, April 4, 2007 9:54 AM
  • Yes, but there're no examples to use parameters
    Wednesday, April 4, 2007 10:16 AM
  • The example given to you has everything needed for you to create a command, add a command binding, and have your code behind you declared in your binding called.

     

    However you'll have to redefine this command on each MenuItem, together with the CommandParameter. You'll receive it in your handler. In the case of the example offered on the link we were talking about:

     

     

    Code Snippet
    public void ExecutedCustomCommand(object sender,
        ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Custom Command Executed with param: " + e.Parameter.ToString());
    }
    

     

    Wednesday, April 4, 2007 10:23 AM
  • Yesssss!!! I didn't know that ExecutedRoutedEventArgs have Parameter property and that I can set its value from XAML simply writting CommandParameter="valuetopass", jajajaja, thank you so much.
    Wednesday, April 4, 2007 12:20 PM
  • Glad it helps Smile
    Wednesday, April 4, 2007 1:25 PM