Здравствуйте, DeKo, Вы писали:
DK>Здравствуйте!
DK>Существует ли готовый контрол для редактирования Хоткеев в WPF?
DK>Или же ручками эдитБокс + PreviewKeyDown ?
Вроде работал нормально:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
public class ShortcutKeyBox : TextBox
{
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
try
{
if (e.Key == Key.LeftCtrl ||
e.Key == Key.RightCtrl ||
e.Key == Key.LeftAlt ||
e.Key == Key.RightAlt ||
e.Key == Key.LeftShift ||
e.Key == Key.RightShift ||
e.Key == Key.System ||
e.Key == Key.Escape ||
e.Key == Key.Tab ||
e.Key == Key.Return)
return;
e.Handled = true;
try
{
var newKeyGesture = new KeyGesture(e.Key, Keyboard.Modifiers);
if (newKeyGesture.Key == Key.Back &&
newKeyGesture.Modifiers == ModifierKeys.None &&
KeyGesture.Key == Key.Back &&
KeyGesture.Modifiers == ModifierKeys.None)
{
KeyGesture = null;
}
else
{
KeyGesture = newKeyGesture;
}
}
catch (NotSupportedException)
{
}
}
finally
{
base.OnPreviewKeyDown(e);
}
}
public KeyGesture KeyGesture
{
get { return (KeyGesture)GetValue(KeyGestureProperty); }
set
{
SetValue(KeyGestureProperty, value);
}
}
public static readonly DependencyProperty KeyGestureProperty =
DependencyProperty.Register("KeyGesture", typeof(KeyGesture), typeof(ShortcutKeyBox),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnKeyGesturePropertyChanged, OnKeyGesturePropertyCoerceValue,
true, UpdateSourceTrigger.PropertyChanged));
private static void OnKeyGesturePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = (ShortcutKeyBox)d;
var keyGesture = e.NewValue as KeyGesture;
if (keyGesture == null)
self.Text = String.Empty;
else
self.Text = keyGesture.GetDisplayStringForCulture(CultureInfo.CurrentCulture);
}
private static object OnKeyGesturePropertyCoerceValue(DependencyObject d, object baseValue)
{
return baseValue;
}
}
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>