Re[2]: [WPF] HotkeyEditBox
От: DeKo  
Дата: 14.07.11 16:42
Оценка:
DK>>Существует ли готовый контрол для редактирования Хоткеев в WPF?
DK>>Или же ручками эдитБокс + PreviewKeyDown ?

V>Вроде работал нормально:



Супер! Спасибо!!! То что надо!



V>
V>using System;
V>using System.Globalization;
V>using System.Windows;
V>using System.Windows.Controls;
V>using System.Windows.Data;
V>using System.Windows.Input;

V>public class ShortcutKeyBox : TextBox
V>{
V>    protected override void OnPreviewKeyDown(KeyEventArgs e)
V>    {
V>        try
V>        {
V>            if (e.Key == Key.LeftCtrl ||
V>                e.Key == Key.RightCtrl ||
V>                e.Key == Key.LeftAlt ||
V>                e.Key == Key.RightAlt ||
V>                e.Key == Key.LeftShift ||
V>                e.Key == Key.RightShift ||
V>                e.Key == Key.System ||
V>                e.Key == Key.Escape ||
V>                e.Key == Key.Tab ||
V>                e.Key == Key.Return)
V>                return;

V>            e.Handled = true;

V>            try
V>            {
V>                var newKeyGesture = new KeyGesture(e.Key, Keyboard.Modifiers);

V>                if (newKeyGesture.Key == Key.Back &&
V>                    newKeyGesture.Modifiers == ModifierKeys.None &&
V>                    KeyGesture.Key == Key.Back &&
V>                    KeyGesture.Modifiers == ModifierKeys.None)
V>                {
V>                    KeyGesture = null;
V>                }
V>                else
V>                {
V>                    KeyGesture = newKeyGesture;
V>                }
V>            }
V>            catch (NotSupportedException)
V>            {
V>            }
V>        }
V>        finally
V>        {
V>            base.OnPreviewKeyDown(e);
V>        }
V>    }

V>    public KeyGesture KeyGesture
V>    {
V>        get { return (KeyGesture)GetValue(KeyGestureProperty); }
V>        set 
V>        { 
V>            SetValue(KeyGestureProperty, value);
V>        }
V>    }

V>    public static readonly DependencyProperty KeyGestureProperty =
V>        DependencyProperty.Register("KeyGesture", typeof(KeyGesture), typeof(ShortcutKeyBox),
V>            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
V>                OnKeyGesturePropertyChanged, OnKeyGesturePropertyCoerceValue,
V>                true, UpdateSourceTrigger.PropertyChanged));

V>    private static void OnKeyGesturePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
V>    {
V>        var self = (ShortcutKeyBox)d;
V>        var keyGesture = e.NewValue as KeyGesture;
V>        if (keyGesture == null)
V>            self.Text = String.Empty;
V>        else
V>            self.Text = keyGesture.GetDisplayStringForCulture(CultureInfo.CurrentCulture);
V>    }

V>    private static object OnKeyGesturePropertyCoerceValue(DependencyObject d, object baseValue)
V>    {
V>        return baseValue;
V>    }
V>}
V>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.