Popular Posts

Tuesday, September 30, 2014

How to filter number in textbox



Below is the complete solution for accept only number and decimal by Textbox  in wpf.


First have create a DP.

 public static class TextBoxFilter
    {
        private static readonly List<Key> _controlKeys = new List<Key>
                                                            {
                                                             
                                                                Key.End,
                                                                Key.Enter,
                                                                Key.Escape,
                                                                Key.Home,
                                                                Key.Insert,
                                                                Key.Left,
                                                                Key.PageDown,
                                                                Key.PageUp,
                                                                Key.Back,
                                                                Key.CapsLock,
                                                                Key.LeftCtrl,
                                                                Key.RightCtrl,
                                                                Key.Down,
                                                                Key.Right,
                                                                Key.LeftShift,
                                                                Key.RightShift,
                                                                Key.Tab,
                                                                Key.Up,
                                                             
                                                           
                                                            };

        private static bool _IsDigit(Key key)
        {
            bool shift = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            bool retValue;
            if (key >= Key.D0 && key <= Key.D9 && !shift || key == Key.OemPeriod)
            {
             
                retValue = true;
            }
            else
            {
                retValue = key >= Key.NumPad0 && key <= Key.NumPad9;
            }
            return retValue;
        }

        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(TextBoxFilter),
            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;
        }


        //==================On the Change Behaviour

        public static bool GetIsBoundOnChange(DependencyObject src)
        {
            return (bool)src.GetValue(IsBoundOnChangeProperty);
        }

        public static void SetIsBoundOnChange(DependencyObject src, bool value)
        {
            src.SetValue(IsBoundOnChangeProperty, value);
        }

        public static DependencyProperty IsBoundOnChangeProperty =
                    DependencyProperty.RegisterAttached(
            "IsBoundOnChange", typeof(bool), typeof(TextBoxFilter),
            new PropertyMetadata(false, IsBoundOnChangeChanged));

        public static void IsBoundOnChangeChanged(DependencyObject src,
                    DependencyPropertyChangedEventArgs args)
        {
            if (src != null && src is TextBox)
            {
                TextBox textBox = src as TextBox;

                if ((bool)args.NewValue)
                {
                    textBox.TextChanged += _TextBoxTextChanged;
                }
            }
        }

        static void _TextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox tb = sender as TextBox;
                BindingExpression binding = tb.GetBindingExpression(TextBox.TextProperty);
                if (binding != null)
                {
                    binding.UpdateSource();
                }
            }
        }
    }


and in XAML.

<TextBox  Name="txtCode"  utility:TextBoxFilter.IsPositiveNumericFilter="True"
                                  utility:TextBoxFilter.IsBoundOnChange="True" />


No comments:

Post a Comment