public static class TextBoxFilters
{
private static readonly List<Key> _controlKeys = new List<Key>
{
Key.Back,
Key.CapsLock,
Key.Ctrl,
Key.Down,
Key.End,
Key.Enter,
Key.Escape,
Key.Home,
Key.Insert,
Key.Left,
Key.PageDown,
Key.PageUp,
Key.Right,
Key.Shift,
Key.Tab,
Key.Up
};
private static bool _IsDigit(Key key)
{
bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
bool retVal;
if (key >= Key.D0 && key <= Key.D9 && !shiftKey)
{
retVal = true;
}
else
{
retVal = key >= Key.NumPad0 && key <= Key.NumPad9;
}
return retVal;
}
}
Next, we need to build our dependency properties. We'll provide a getter and setter and the registration. When the property is set, we'll check to see if the control is a TextBox . If it is, we hook into the KeyDown event and suppress it. The code looks like this: public static bool GetIsPositiveNumericFilter(DependencyObject src)
{
return (bool) src.GetValue(IsPositiveNumericFilterProperty);
}
public static void SetIsPositiveNumericFilter(DependencyObject src, bool value)
{
src.SetValue(IsPositiveNumericFilterProperty, value);
}
public static DependencyProperty IsPositiveNumericFilterProperty = DependencyProperty.RegisterAttached(
"IsPositiveNumericFilter", typeof (bool), typeof (TextBoxFilters),
new PropertyMetadata(false, IsPositiveNumericFilterChanged));
public static void IsPositiveNumericFilterChanged(DependencyObject src, DependencyPropertyChangedEventArgs args)
{
if (src != null && src is TextBox)
{
TextBox textBox = src as TextBox;
if ((bool)args.NewValue)
{
textBox.KeyDown += _TextBoxPositiveNumericKeyDown;
}
}
}
static void _TextBoxPositiveNumericKeyDown(object sender, KeyEventArgs e)
{
bool handled = true;
if (_controlKeys.Contains(e.Key) || _IsDigit(e.Key))
{
handled = false;
}
e.Handled = handled;
}
|