我想从测试中提出一个命令。我需要模拟 RibbonMenuItem 是 OriginalSource。不幸的是,我无法访问原始的 RibbonBar,因此也无法访问 RibbonMenuItem。因此,我在测试中创建了一个新的
RibbonMenuItem menuItem = new RibbonMenuItem();
menuItem.Command = Commands.AddCommand;
menuItem.CommandBindings.Add(new CommandBinding Commands.AddCommand));
menuItem.Header = "foobar";
(Commands 是一个包含我实现的所有命令的类。)
然后我尝试使用以下行引发命令:
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 只是像 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;
}
// ...
}