Решение не короткое !!
Вам нужно создать зависимость (здесь GestureMenuItem)
в файле xaml
xmlns:common="clr-namespace:Common.Caliburn"
:
:
<Menu IsMainMenu="true" DockPanel.Dock="Top">
<common:GestureMenuItem x:Name="Save" Key="S" Modifiers="Ctrl" Header="_Save"/>
в файле ActionMessageCommand.cs
using System;
using System.Windows.Input;
using Caliburn.Micro;
namespace Common.Caliburn
{
public class ActionMessageCommand : ActionMessage, ICommand
{
static ActionMessageCommand()
{
EnforceGuardsDuringInvocation = true;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
}
void ICommand.Execute(object parameter)
{
}
public event EventHandler CanExecuteChanged;
}
}
в файле GestureMenuItem.cs
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace Common.Caliburn
{
public class GestureMenuItem : MenuItem
{
public override void EndInit()
{
Interaction.GetTriggers(this).Add(ConstructTrigger());
if(string.IsNullOrEmpty(InputGestureText))
InputGestureText = BuildInputGestureText(Modifiers, Key);
base.EndInit();
}
private static readonly IEnumerable<ModifierKeys> ModifierKeysValues = Enum.GetValues(typeof(ModifierKeys)).Cast<ModifierKeys>().Except(new [] { ModifierKeys.None });
private static readonly IDictionary<ModifierKeys, string> Translation = new Dictionary<ModifierKeys, string>
{
{ ModifierKeys.Control, "Ctrl" }
};
private static string BuildInputGestureText(ModifierKeys modifiers, Key key)
{
var result = new StringBuilder();
foreach (var val in ModifierKeysValues)
if ((modifiers & val) == val)
result.Append((Translation.ContainsKey(val) ? Translation[val] : val.ToString()) + " + ");
result.Append(key);
return result.ToString();
}
private TriggerBase<FrameworkElement> ConstructTrigger()
{
var trigger = new InputBindingTrigger();
trigger.GlobalInputBindings.Add(new KeyBinding { Modifiers = Modifiers, Key = Key });
var command = new ActionMessageCommand { MethodName = Name };
Command = command;
trigger.Actions.Add(command);
return trigger;
}
public static readonly DependencyProperty ModifiersProperty =
DependencyProperty.Register("Modifiers", typeof(ModifierKeys), typeof(GestureMenuItem), new PropertyMetadata(default(ModifierKeys)));
public ModifierKeys Modifiers
{
get { return (ModifierKeys)GetValue(ModifiersProperty); }
set { SetValue(ModifiersProperty, value); }
}
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register("Key", typeof(Key), typeof(GestureMenuItem), new PropertyMetadata(default(Key)));
public Key Key
{
get { return (Key)GetValue(KeyProperty); }
set { SetValue(KeyProperty, value); }
}
}
}
в файле InputBindingTrigger.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Caliburn.Micro;
namespace Common.Caliburn
{
public class InputBindingTrigger : TriggerBase<FrameworkElement>, ICommand
{
public InputBindingTrigger()
{
GlobalInputBindings = new BindableCollection<InputBinding>();
LocalInputBindings = new BindableCollection<InputBinding>();
}
public static readonly DependencyProperty LocalInputBindingsProperty =
DependencyProperty.Register("LocalInputBindings", typeof(BindableCollection<InputBinding>), typeof(InputBindingTrigger), new PropertyMetadata(default(BindableCollection<InputBinding>)));
public BindableCollection<InputBinding> LocalInputBindings
{
get { return (BindableCollection<InputBinding>)GetValue(LocalInputBindingsProperty); }
set { SetValue(LocalInputBindingsProperty, value); }
}
public BindableCollection<InputBinding> GlobalInputBindings
{
get { return (BindableCollection<InputBinding>)GetValue(GlobalInputBindingProperty); }
set { SetValue(GlobalInputBindingProperty, value); }
}
public static readonly DependencyProperty GlobalInputBindingProperty =
DependencyProperty.Register("GlobalInputBinding", typeof(BindableCollection<InputBinding>), typeof(InputBindingTrigger), new UIPropertyMetadata(null));
protected override void OnAttached()
{
foreach (var binding in GlobalInputBindings.Union(LocalInputBindings))
binding.Command = this;
AssociatedObject.Loaded += delegate
{
var window = GetWindow(AssociatedObject);
foreach (var binding in GlobalInputBindings)
window.InputBindings.Add(binding);
foreach (var binding in LocalInputBindings)
AssociatedObject.InputBindings.Add(binding);
};
base.OnAttached();
}
private Window GetWindow(FrameworkElement frameworkElement)
{
if (frameworkElement is Window)
return frameworkElement as Window;
var parent = frameworkElement.Parent as FrameworkElement;
return GetWindow(parent);
}
bool ICommand.CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
InvokeActions(parameter);
}
}
}
и в ShellViewModel.cs, используя условное имя Caliburn
public void Save()
{
//some code here
}