我有一个程序集,我在类中定义Application wide命令:
public class CtrlCommands
{
public static readonly RoutedUICommand CtrlAddToFavourites = new RoutedUICommand("CtrlAddToFavourites", "CtrlAddToFavourites", typeof(CtrlCommands));
public static readonly RoutedUICommand CtrlRemoveFromFavourites = new RoutedUICommand("CtrlRemoveFromFavourites", "CtrlRemoveFromFavourites", typeof(CtrlCommands));
public static readonly RoutedUICommand CtrlNew = new RoutedUICommand("CtrlNew", "CtrlNew", typeof(CtrlCommands));
public static readonly RoutedUICommand CtrlDelete = new RoutedUICommand("CtrlDelete", "CtrlDelete", typeof(CtrlCommands));
public static readonly RoutedUICommand CtrlSearch = new RoutedUICommand("CtrlSearch", "CtrlSearch", typeof(CtrlCommands));
}
在我的示例中,我在代码中生成自定义控件(Control派生自按钮)并分配命令(CtrlRibbonItem extends Button):
mainPage.CtrlRibbonItems.Add(new CtrlRibbonItem { CtrlIcon = "file", Content = "Neu", Command = CtrlCommands.CtrlNew});
功能区项目始终处于非活动状态,并且永远不会调用canExecute处理程序。
在应用程序本身中,命令就像这样连接:
<mvvm:CtrlBaseUserControl.CommandBindings>
<CommandBinding x:Name="CtrlNewCommandBinding"
Command="commands:CtrlCommands.CtrlNew"
CanExecute="CtrlNewCommandBinding_OnCanExecute"
Executed="CtrlNewCommandBinding_OnExecuted"/>
</mvvm:CtrlBaseUserControl.CommandBindings>
当我使用普通的WPF按钮时,一切正常......
更新 问题是在UserControl.CommandBindings中定义了命令绑定,而CtrlRibbonItem放在UserControl之外(功能区栏是窗口的一部分,而不是用户控件)