RibbonSplitButton Command is executed twice
Asked Answered
R

5

5

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?

Rudder answered 9/1, 2014 at 8:50 Comment(0)
A
6

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

Alexandraalexandre answered 9/1, 2014 at 11:46 Comment(0)
L
5

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();
}
Longlegged answered 4/12, 2014 at 10:9 Comment(1)
This is the best answer as it conforms to the way WPF works.Phobia
R
1

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

Rudder answered 9/1, 2014 at 13:22 Comment(0)
C
0

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.

Cortex answered 18/3, 2015 at 20:18 Comment(0)
S
0

I fixed it by using a RibbonMenuItem instead of RibbonSplitMenuItem.

Shovelboard answered 11/5, 2023 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.