WPF ip地址掩码文本框

时间:2014-09-15 08:00:47

标签: c# wpf textbox

我正在尝试在wpf中构建ip文本框字段。我的目标是,在Windows IP文本框上构建相同的内容 Windows ip textbox

我的ip文本框版本如下:

   public class IpInputBox : TextBox
    {
        private MaskedTextProvider _mprovider = null;
        private bool _previousInsertState = false;
        private bool _insertIsON = false;
        private bool _stayInFocusUntilValid = true;
        private bool _newTextIsOk = false;
        private bool _ignoreSpace = true;

        public IpInputBox()
        {
            //TextWrapping = TextWrapping.Wrap;
            HorizontalAlignment = HorizontalAlignment.Stretch;
        }

        public string Mask
        {
            get
            {
                if (_mprovider != null) return _mprovider.Mask;
                return "";
            }
            set
            {
                _mprovider = new MaskedTextProvider(value);
                _mprovider.PromptChar = ' ';
                Text = _mprovider.ToDisplayString();
            }
        }


        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (this.SelectionLength > 1)
            {
                this.SelectionLength = 0;
                e.Handled = true;
            }

            //Debug.WriteLine(Text.Substring(CaretIndex, 1));
            //Debug.WriteLine(Text.Substring(CaretIndex, 1) == String.Empty);
            //if (e.Key != Key.Right || e.Key != Key.Left && Text.Substring(CaretIndex, 1) == String.Empty)
            //{
            //    e.Handled = true;
            //}

            if (e.Key != Key.NumPad0 && e.Key != Key.NumPad1 && e.Key != Key.NumPad2 && e.Key != Key.NumPad3 && e.Key != Key.NumPad4 && e.Key != Key.NumPad5 &&
                e.Key != Key.NumPad6 && e.Key != Key.NumPad7 && e.Key != Key.NumPad8 && e.Key != Key.NumPad9 && e.Key != Key.D0 && e.Key != Key.D1 && e.Key != Key.D2 &&
                e.Key != Key.D3 && e.Key != Key.D3 && e.Key != Key.D4 && e.Key != Key.D5 && e.Key != Key.D6 && e.Key != Key.D7 && e.Key != Key.D8 && e.Key != Key.D9 &&
                e.Key != Key.Right && e.Key != Key.Left)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            MaskedTextResultHint hint;
            int TestPosition;

            if (e.Text.Length == 1)
                this._newTextIsOk = _mprovider.VerifyChar(e.Text[0], this.CaretIndex, out hint);
            else
                this._newTextIsOk = _mprovider.VerifyString(e.Text, out TestPosition, out hint);

            base.OnPreviewTextInput(e);
        }

        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            string PreviousText = this.Text;
            if (_newTextIsOk)
            {
                base.OnTextInput(e);
                if (_mprovider.VerifyString(this.Text) == false) this.Text = PreviousText;
                while (!_mprovider.IsEditPosition(this.CaretIndex) && _mprovider.Length > this.CaretIndex) this.CaretIndex++;
            }
            else
                e.Handled = true;
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            if (!_insertIsON)
            {
                PressKey(Key.Insert);
                _insertIsON = true;
            }
        }

        protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            if (_stayInFocusUntilValid)
            {
                _mprovider.Clear();
                _mprovider.Add(Text);

                //Prevent to lose focus, when wrong inputs
                if (!_mprovider.MaskFull) e.Handled = true;
            }

            base.OnPreviewLostKeyboardFocus(e);
        }

        /// <summary>
        /// When the textbox looses its focus we need to return the Insert Key state to its previous state
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnLostFocus(e);

            if (_previousInsertState != Keyboard.PrimaryDevice.IsKeyToggled(Key.Insert))
                PressKey(Key.Insert);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            Debug.WriteLine(_mprovider.PromptChar);

            Debug.WriteLine(_mprovider.PromptChar);
        }

        private void PressKey(Key key)
        {
            KeyEventArgs eInsertBack = new KeyEventArgs(Keyboard.PrimaryDevice,
                                                        Keyboard.PrimaryDevice.ActiveSource,
                                                        0, key);
            eInsertBack.RoutedEvent = KeyDownEvent;
            InputManager.Current.ProcessInput(eInsertBack);
        }
    }

当我在里面写下ip地址时,该字段将会延伸。 Custom ip textobx 如何在wpf中构建我的ip文本框,就像在windows中一样的ip文本框?

1 个答案:

答案 0 :(得分:2)

我认为windows IP Textbox实际上是4个单独的Textbox。每个都接受0到255之间的数字输入。

这样,通过为每个Textbox提供4个单独的属性来实现它更简单。 固定宽度,足以容纳长度为3的数字。

最后,小数可能只是Textblocks或Labels。它只是一个样式化的问题,看起来像一个透明的文本框,并添加一个包裹它们的边框。