public override void OnApplyTemplate() { base.OnApplyTemplate(); TextBox hotKeyText = this.Template.FindName("PART_HotKeyText", this) as TextBox; if (hotKeyText != null) { hotKeyText.Text = HotKeyText; hotKeyText.PreviewKeyDown += new KeyEventHandler((sender, e) => { HotKey.ControlKeys control = HotKey.ControlKeys.None; Key key = Key.None; foreach (Key kkey in Enum.GetValues(typeof(Key))) { if (!ignoredKeys.Contains(kkey)) { if (Keyboard.IsKeyDown(kkey)) { if (keyMap.ContainsKey(kkey)) { control |= keyMap[kkey]; } else { key = kkey; } } } } HotKey = new HotKey(control, key); e.Handled = true; }); } Button clear = this.Template.FindName("PART_Clear", this) as Button; //Button clear = GetTemplateChild("PART_Clear") as Button; if (clear != null) { clear.Click += new RoutedEventHandler((sender, e) => { HotKey = null; }); } }
public override void OnApplyTemplate() { base.OnApplyTemplate(); TextBox hotKeyText = this.Template.FindName("PART_HotKeyText", this) as TextBox; if (hotKeyText != null) { hotKeyText.Text = HotKeyText; hotKeyText.PreviewKeyDown += new KeyEventHandler((sender, e) => { HotKey.ControlKeys control = HotKey.ControlKeys.None; Key key = Key.None; foreach (Key kkey in Enum.GetValues(typeof(Key))) { if (!ignoredKeys.Contains(kkey)) { if (Keyboard.IsKeyDown(kkey)) { if (keyMap.ContainsKey(kkey)) { control |= keyMap[kkey]; } else { key = kkey; } } } } if (key.ToString() == "Back") { HotKey = null; hotKeyText.Text = "无"; } else { HotKey = new HotKey(control, key); } e.Handled = true; }); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="control">控制键</param> /// <param name="key">主键</param> public HotKey(HotKey.ControlKeys control, Key key) { ControlKey = control; Key = key; KeyId = (int)ControlKey + (int)Key * 100; }