Friday, August 26, 2011

WPF RibbonControl : RibbonApplicationSplitMenuItem triggers click command twice

In WPF RibbonControl 4.0, single menu click on RibbonApplicationSplotMenuItem caused click event triggers twice. This means that if the menu is supposed to show a dialog, two same dialogs are going to pop up.

Here is an xaml that is using RibbonApplicationSplotMenuItem.

<ribbon:RibbonApplicationSplitMenuItem x:Uid="saveAsSplitMenu" Header="Save As"
                         Command="SaveAs" CommandParameter="SaveAsFile">
   <ribbon:RibbonApplicationMenuItem x:Uid="saveAsDoc" Header="Save As Doc"                                                
                         Command="SaveAs" CommandParameter="Doc" />
   <ribbon:RibbonApplicationMenuItem x:Uid="saveAsTxt" Header="Save As Text"
                         Command="SaveAs" CommandParameter="Text" />
</ribbon:RibbonApplicationSplitMenuItem>


And let's say we registered CommandBinding for built-in ApplicationCommands.SaveAs command as follows.


CommandManager.RegisterClassCommandBinding(typeof(RibbonWindow),
    new CommandBinding(ApplicationCommands.SaveAs, ExecutedSaveAsCommand, CanExecuteSaveAs));



ExecutedCommand handler is defined as follows.

private void ExecutedSaveAsCommand(object sender, ExecutedRoutedEventArgs e)
{         
    ....
    switch(e.Parameter.ToString())
    {
       case "...": 
         break;
         ... 
       case "SaveAsFile":
         if (e.OriginalSource.GetType() == 
             typeof(RibbonApplicationSplitMenuItem))
         {
            this.SaveAsFile();
         }
         break;
    }
}

RibbonApplicationSplitMenuItem triggers two events with different OriginalSource: one for RibbonButton and the other for RibbonApplicationSplitMenuItem. The example above shows event filtering of RibbonApplicationSplitMenuItem source. This might be considered as workaround until it will be fixed(?) later in RibbonControlLibary.

No comments:

Post a Comment