Looking at the MenuItem in the reflector, we see how the MenuItem receives Header / InputGesture values ββthat:
private static object CoerceInputGestureText(DependencyObject d, object value) { RoutedCommand command; MenuItem item = (MenuItem) d; if ((string.IsNullOrEmpty((string) value) && !item.HasNonDefaultValue(InputGestureTextProperty)) && ((command = item.Command as RoutedCommand) != null)) { InputGestureCollection inputGestures = command.InputGestures;
There is similar code to force the use of the header property based on the current command, but in this case it searches for RoutedUICommand . This tells us that the commands must be an instance of RoutedCommand / RoutedUICommand in order to use this MenuItem function.
Looking at the RoutedCommand in the reflector, there is no easy way to create a DelegateCommand that comes from the RoutedCommand , because the CanExecute / Execute methods are not virtual.
We could write something like:
public class DelegateCommand : RoutedCommand, ICommand { bool ICommand.CanExecute(object parameter) {
But this does not stop calling the implicit CanExecute / Execute methods on the RoutedCommand . This may not be a problem.
Alternatively, we can create a custom MenuItem that is smart enough to look for our DelegateCommand (or somewhere else) and use its text / gestures.
public class MyMenuItem : MenuItem { static MyMenuItem() { InputGestureTextProperty.OverrideMetadata(typeof(MyMenuItem), new FrameworkPropertyMetadata(string.Empty, null, CoerceInputGestureText)); } private static object CoerceInputGestureText(DependencyObject d, object value) { MenuItem item = (MenuItem)d; var command = item as DelegateCommand; if ((string.IsNullOrEmpty((string)value) && DependencyPropertyHelper.GetValueSource(item, InputGestureTextProperty).BaseValueSource == BaseValueSource.Default && command != null) { InputGestureCollection inputGestures = command.InputGestures;
source share