RibbonSplitButton Command is executed twice

2k Views Asked by At

I am using the Ribbons Control Libary from Microsoft for WPF to privide a Ribbon in our WPF Application.

We use Splitbuttons in the following way in the XAML Part:

<r:RibbonSplitButton Label="SplitButtonLabel" LargeImageSource="..." Command="{Binding SplitButtonCommand}">
  <r:RibbonSplitMenuItem Header="Item 1" ImageSource="..." Command="{Binding Command1}"/>
  <r:RibbonSplitMenuItem Header="Item 2" ImageSource="..." Command="{Binding Command2}"/>
  <r:RibbonSplitMenuItem Header="Item 3" ImageSource="..." Command="{Binding Command3}"/>
</r:RibbonSplitButton>

If i click on the Upper Part of the Split Button the Command SplitButtonCommand is executed once as it normally.

If I click on the Bottom Part of the SplitButton and then any Menü Item (e.g. Item 1) the Command of this Item is executed twice.

Does anyone have any clue causes the Problem?

5

There are 5 best solutions below

0
On BEST ANSWER

It appears it maybe by design have a look at this article. There is a workaround mentioned:

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.

RibbonMenuItem triggers command twice

0
On

As dellywheel says it seems to be that this behavior is by design.

I dealt with the Problem changing my Code like the following Example.

<r:RibbonSplitButton Label="SplitButtonLabel" LargeImageSource="..." Command="{Binding SplitButtonCommand}">
  <r:RibbonButton Label="Item 1" SmallImageSource="..." Command="{Binding Command1}"/>
  <r:RibbonButton Label="Item 2" SmallImageSource="..." Command="{Binding Command2}"/>
  <r:RibbonButton Label="Item 3" SmallImageSource="..." Command="{Binding Command3}"/>
</r:RibbonSplitButton>

I replaced the usage of RibbonSplitMenuItem by using RibbonButtons with a provided SmallImageSource

1
On

Just another workaround, you can use the click event:

<r:RibbonSplitButton Label="SplitButtonLabel" LargeImageSource="..." Click="Split_Click">
  <r:RibbonSplitMenuItem Header="Item 1" ImageSource="..." Click="Click_1"/>
  <r:RibbonSplitMenuItem Header="Item 2" ImageSource="..." Click="Click_2"/>
  <r:RibbonSplitMenuItem Header="Item 3" ImageSource="..." Click="Click_3"/>
</r:RibbonSplitButton>


And inside the click event handler, set the Handled property to true:

private void Click_1(object sender, RoutedEventArgs e)
{
    e.Handled = true;
    ((YourViewModel)DataContext).Command1();
}
0
On

I fixed it by using a RibbonMenuItem instead of RibbonSplitMenuItem.

0
On

As this is a bug by design :), convert the Template property to a new resource and remove the TemplateBindings from the PART_HeaderButton's COMMAND, COMMANDPARAMETER and COMMANDTARGET (ie remove these 3 attributes altogether), as they are the source of the "feature" causing the duplicate invocation of the command.

You can apply this ControlTemplate to all buttons, if you like.