我想从测试中提出一个命令。我需要模拟RibbonMenuItem是OriginalSource。不幸的是,我没有任何访问原始RibbonBar的权限,因此也没有访问RibbonMenuItem。因此,我在测试中创建了一个新的
RibbonMenuItem menuItem = new RibbonMenuItem();
menuItem.Command = Commands.AddCommand;
menuItem.CommandBindings.Add(new CommandBinding Commands.AddCommand));
menuItem.Header = "foobar";
(命令是一个包含我实现的所有命令的类。)
然后我尝试使用以下行提升命令:
Commands.AddCommand.Execute(null, menuItem);
但这不起作用。
创建测试项目后,我现在知道为什么不起作用,但我不知道如何解决。这是我的测试项目的代码
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>
视图模型
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;
}
}
当我将RibbonMenuItem rmi添加到RibbonSplitButton时,执行该命令,否则不执行。因为在我的实际项目中,RibbonMenuItem只是&#34;挂在&#34;正如rmi所做的那样,命令不会被提升。
如上所述,我在测试项目中没有任何对RibbonBar本身的访问权限,否则我会直接使用现有的RibbonMenuItem。
您对如何使命令有效有任何想法吗?
private void OnExecuteAdd(object sender, ExecutedRoutedEventArgs e)
{
RibbonMenuItem originalSource = e.OriginalSource as RibbonMenuItem;
if (originalSource != null)
{
string header = originalSource.Header as string;
}
// ...
}