2

I want to raise a Command from a test. I need to simulate that a RibbonMenuItem is the OriginalSource. Unfortunately I do not have any access to the original RibbonBar and thus also not to a RibbonMenuItem. Accordingly I create a new one in my test

RibbonMenuItem menuItem = new RibbonMenuItem();
menuItem.Command = Commands.AddCommand;
menuItem.CommandBindings.Add(new CommandBinding Commands.AddCommand));
menuItem.Header = "foobar";

(Commands is a class containing all Commands I implemented.)

Then I try to raise the command with the following line:

Commands.AddCommand.Execute(null, menuItem);

But this does not work.

After creating a test project I now know why that does not work, though I don't know how to workaround. Here the code of my test project

XAML:

<Window x:Class="MakeOrBreak.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <ribbon:Ribbon Grid.Row="0">
      <ribbon:RibbonSplitButton x:Name="splitButton" Label="Test">
        <ribbon:RibbonMenuItem x:Name="item1" Header="Item1" />
        <ribbon:RibbonMenuItem x:Name="item2" Header="Item2" />
      </ribbon:RibbonSplitButton>
    </ribbon:Ribbon>
    <TextBlock Grid.Row="1" x:Name="textblock" />
  </Grid>
</Window>

ViewModel

public partial class MainWindow : Window
  {
    private int counter = 1;

    public static RoutedCommand ClickCommand = new RoutedCommand();

    public MainWindow()
    {
      InitializeComponent();

      CommandBinding commandBinding = new CommandBinding(ClickCommand, ExecuteClickCommand, CanExecuteClickCommand);

      splitButton.Command = ClickCommand;
      splitButton.CommandBindings.Add(commandBinding);

      RibbonMenuItem rmi = new RibbonMenuItem();
      rmi.Command = ClickCommand;
      rmi.CommandBindings.Add(new System.Windows.Input.CommandBinding(ClickCommand));
      rmi.Header = "foobar";

      // Without this line the command at the last line isn't automatically raised
      // splitButton.Items.Add(rmi);

      Style baseMenuItemStyle = splitButton.TryFindResource(typeof(Microsoft.Windows.Controls.Ribbon.RibbonMenuItem)) as Style;
      if (baseMenuItemStyle == null)
      {
        baseMenuItemStyle = new Style(typeof(Microsoft.Windows.Controls.Ribbon.RibbonMenuItem));
      }
      Style ribbonItemsStyle = new Style(typeof(Microsoft.Windows.Controls.Ribbon.RibbonMenuItem), baseMenuItemStyle);
      ribbonItemsStyle.Setters.Add(new Setter(Microsoft.Windows.Controls.Ribbon.RibbonMenuItem.CommandProperty, ClickCommand));
      splitButton.Resources.Add(typeof(Microsoft.Windows.Controls.Ribbon.RibbonMenuItem), ribbonItemsStyle);

      // simulate Click
      ClickCommand.Execute(null, rmi);
    }

    private void ExecuteClickCommand(object sender, ExecutedRoutedEventArgs e)
    {
      RibbonMenuItem originalSource = e.OriginalSource as RibbonMenuItem;
      if (originalSource != null)
      {
        textblock.Text = "Item Pressed " + counter++;
      }
      e.Handled = true;
    }

    private void CanExecuteClickCommand(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
      e.Handled = true;
    }
}

When I add the RibbonMenuItem rmi to the RibbonSplitButton the command is executed otherwise not. Since in my actual project the RibbonMenuItem is just "hanging around" as rmi does here the command is not raised.

As already said I do not have any access to the RibbonBar itself in my test project otherwise I would directly take an existing RibbonMenuItem.

Do you have any ideas how to get the command working?


private void OnExecuteAdd(object sender, ExecutedRoutedEventArgs e)
{
  RibbonMenuItem originalSource = e.OriginalSource as RibbonMenuItem;
  if (originalSource != null)
  {
    string header = originalSource.Header as string;
  }
  // ...
}
Leri
  • 12,367
  • 7
  • 43
  • 60
Em1
  • 1,077
  • 18
  • 38

0 Answers0