none
RibbonApplicationSplitMenuItem triggers command twice

    Question

  • I created a ribbon window with a ribbon using the RibbonControlsLibrary.dll in Visual Studio 2010 with the .NET Framework 4.0. The application menu of my ribbon contains RibbonApplicationSplitMenuItems like the one below.

                        <ribbon:RibbonApplicationSplitMenuItem Name="btnPrint" Command="ApplicationCommands.Print"

                                Header="{local:cedTranslate Print}" HeaderKeyTip="{local:cedTranslate PrintHeaderKeyTip}"

                                KeyTip="{local:cedTranslate PrintKeyTip}" ToolTipTitle="{local:cedTranslate PrintTitle}"

                                ToolTipDescription="{local:cedTranslate PrintDescription}"

                                DropDownToolTipTitle="{local:cedTranslate PrintDropDownTitle}"

                                ImageSource="/CardExchangeDesignerX;component/Images/Print.png">

                            <ribbon:RibbonApplicationMenuItem Name="btnPrint2" Command="ApplicationCommands.Print"

                                    KeyTip="{local:cedTranslate PrintKeyTip}" ToolTipTitle="{local:cedTranslate Print}"

                                    ToolTipDescription="{local:cedTranslate PrintDescription2}"

                                    ImageSource="/CardExchangeDesignerX;component/Images/Print.png" />

                            <ribbon:RibbonApplicationMenuItem Name="btnQuickPrint" Command="ApplicationCommands.Print"

                                    KeyTip="{local:cedTranslate QuickPrintKeyTip}"

                                    ToolTipTitle="{local:cedTranslate QuickPrintTitle}"

                                    ToolTipDescription="{local:cedTranslate QuickPrintDescription}"

                                    ImageSource="/CardExchangeDesignerX;component/Images/QuickPrint.png" />

                            <ribbon:RibbonApplicationMenuItem Name="btnPrintPreview"

                                    Command="ApplicationCommands.PrintPreview"

                                    KeyTip="{local:cedTranslate PrintPreviewKeyTip}"

                                    ToolTipTitle="{local:cedTranslate PrintPreviewTitle}"

                                    ToolTipDescription="{local:cedTranslate PrintPreviewDescription}"

                                    ImageSource="/CardExchangeDesignerX;component/Images/PrintPreview.png" />

                        </ribbon:RibbonApplicationSplitMenuItem>

     

     

    When I click on the btnPrint button, the executed event of the command binding of the print command is called twice. Why does that happen? How can I avoid that?


    Rutger Koperdraad.
    Monday, May 23, 2011 11:22 PM

Answers

  • Hi Rutger Koperdraad,

    Based on my research, this behavior is by design. The UIElement which receive the Command is RibbonButton at the first time and the second is RibbonApplicationSplitMenuItems.

    Although this is the nature of RibbonControl you can try to workaround this by parsing the ExecutedRoutedEventArgs and check if the OriginalSource is the same as Source, if yes then get this command executed.

    You can also post the suggestions to our Connect feedback portal. Our developer will evaluate them seriously and take them into consideration when designing future release of the product.

    https://connect.microsoft.com/WPF

    Improving the quality of our products and services is a never ending process for Microsoft .

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 24, 2011 3:17 PM
    Moderator
  • Hi Yves,

    I actually solved the problem by editing the source code of the RibbonControlsLibrary.dll. In the file Generic.xaml, I looked up the control template of the RibbonApplicationSplitMenuItem. The template contains a RibbonButton name PART_HeaderButton, whose Command, CommandParameter and CommandTarget properties are set with a TemplateBinding to the respective properties of the RibbonApplicationSplitMenuItem. It is redundant to have the command be triggered by the button, because the underlying menu item already takes care of that. Removing the three template bindings solves the problem.

    regards,


    Rutger Koperdraad.
    • Marked as answer by cucucucu Friday, June 24, 2011 10:10 PM
    Friday, June 24, 2011 10:10 PM

All replies

  • Hi Rutger Koperdraad,

    Based on my research, this behavior is by design. The UIElement which receive the Command is RibbonButton at the first time and the second is RibbonApplicationSplitMenuItems.

    Although this is the nature of RibbonControl you can try to workaround this by parsing the ExecutedRoutedEventArgs and check if the OriginalSource is the same as Source, if yes then get this command executed.

    You can also post the suggestions to our Connect feedback portal. Our developer will evaluate them seriously and take them into consideration when designing future release of the product.

    https://connect.microsoft.com/WPF

    Improving the quality of our products and services is a never ending process for Microsoft .

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 24, 2011 3:17 PM
    Moderator
  • Thanks, Yves. I posted the issue on the Connect site, but the work around is straightforward and works excellent. Once more, thanks a lot for your help.
    Rutger Koperdraad.
    Tuesday, May 24, 2011 5:03 PM
  • You are welcome! Rutger :)

    And thank you for your feedback. We will make this better when designing future release of the product.

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 25, 2011 8:26 AM
    Moderator
  • Hi Yves,

    I remodeled my application to use the MVVM design pattern according to this article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx. That works great in many aspects, but with their relaying command logic I lost the information about the source and the original source of the command, and again, clicking on a RibbonApplicationSplitMenuItems causes the command to fire twice. Do you also have a magic suggestion for this case? Can I have access to the command source somehow? Thanks a lot.

    The RelayCommand class I use is this:

    Friend Class cedRelayCommand
        Implements ICommand

        Private m_dlgCanExecute As Predicate(Of Object)
        Private m_dlgExecute As Action(Of Object)

    #Region " Constructors "

        Public Sub New(ByVal dlgExecute As Action(Of Object))
            Call Me.New(dlgExecute, Nothing)
        End Sub

        Public Sub New(ByVal dlgExecute As Action(Of Object), ByVal dlgCanExecute As Predicate(Of Object))
            If dlgExecute Is Nothing Then
                Throw New ArgumentNullException("dlgExecute")
            End If

            m_dlgExecute = dlgExecute
            m_dlgCanExecute = dlgCanExecute
        End Sub

    #End Region

    #Region " ICommand implementation "

        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
            Return If(m_dlgCanExecute Is Nothing, True, m_dlgCanExecute(parameter))
        End Function

        Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
            AddHandler(ByVal value As EventHandler)
                AddHandler CommandManager.RequerySuggested, value
            End AddHandler

            RemoveHandler(ByVal value As EventHandler)
                RemoveHandler CommandManager.RequerySuggested, value
            End RemoveHandler

            RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)

            End RaiseEvent
        End Event


        Public Sub Execute(parameter As Object) Implements ICommand.Execute
            Call m_dlgExecute(parameter)
        End Sub

    #End Region

    End Class

    The way I typically use it in the ViewModel classes is like this:

        Private m_objSelectCommand As New cedRelayCommand(AddressOf SelectCommandExecuted)

        Public ReadOnly Property SelectCommand() As ICommand
            Get
                Return m_objSelectCommand
            End Get
        End Property

        Private Sub SelectCommandExecuted(ByVal objParameter As Object)
             ...
        End Sub

    The SelectCommand property is used for data binding the Command property of the ribbon menu items.


    Rutger Koperdraad.
    Friday, June 17, 2011 2:02 AM
  • Hi Yves,

    I actually solved the problem by editing the source code of the RibbonControlsLibrary.dll. In the file Generic.xaml, I looked up the control template of the RibbonApplicationSplitMenuItem. The template contains a RibbonButton name PART_HeaderButton, whose Command, CommandParameter and CommandTarget properties are set with a TemplateBinding to the respective properties of the RibbonApplicationSplitMenuItem. It is redundant to have the command be triggered by the button, because the underlying menu item already takes care of that. Removing the three template bindings solves the problem.

    regards,


    Rutger Koperdraad.
    • Marked as answer by cucucucu Friday, June 24, 2011 10:10 PM
    Friday, June 24, 2011 10:10 PM