I'm adding now some shortcut keys to my wpf application. I have one ComboBox and I want each item to be able to have a shortcut key. Now, I do this with custom class deriving from ComboBoxItem and having a property ShortcutKey, which register the access key via AccessKeyManager on set.
public class ComboBoxItemLanguage : ComboBoxItem { private string _key; private ComboBox parent; public string LanguageCode { get; set; } public string ShortcutKey { get { return _key; } set { _key = value; AccessKeyManager.Register(value, this); AccessKeyManager.AddAccessKeyPressedHandler(this, AccessKeyPressedEventHandler); } } public ComboBoxItemLanguage() { } private void AccessKeyPressedEventHandler(object sender, AccessKeyPressedEventArgs e) { parent = parent ?? ItemsControl.ItemsControlFromItemContainer(this) as ComboBox; // gets the parent ComboBox parent.SelectedIndex = parent.Items.IndexOf(this); e.Handled = true; } }
Then, in XAML it looks like this:
<ComboBox x:Name="cb_lang" SelectedValuePath="LanguageCode" SelectedValue="{Binding InputLanguage, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"><local:ComboBoxItemLanguage Content="English" LanguageCode="en" ShortcutKey="e"/></ComboBox>But always when I press the alt+something shortcut key, the annoying Windows beep sound is played as it had not been registered. Anyone knows, how to get rid of it? Or should I use commands? (But I think this solution is more re-usable).