自定义控件的bug

时间:2018-02-05 17:56:01

标签: c# wpf xaml

我有一个继承自文本框控件的自定义控件。除了一件事,它按预期工作。当用户输入特殊字符时,光标返回到文本框的第一个位置,而不是停留在特殊字符被剥离的位置。关于如何解决这个问题的任何想法?

以下是控件的完整代码:

using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace GamePresenter.XamlControls
{
    class CleanTextBox : TextBox
    {
        #region Constructors
        public CleanTextBox()
        {
            TextChanged += new TextChangedEventHandler(OnTextChanged);
            KeyDown += new KeyEventHandler(OnKeyDown);
        }
        #endregion

        #region Properties
        new public String Text {
            get { return base.Text; }
            set {
                base.Text = LeaveOnlyAlphaNumereicAndSpace(value);
            }
        }
        #endregion

        #region Functions
        private bool isAlphaNumericOrKeypad(Key inKey)
        {
            //It will only enter the if statement if we are NOT dealing with alphanumeric
            if ((inKey < Key.D0 || inKey > Key.D9) && (inKey < Key.A || inKey > Key.Z) && (inKey != Key.Space))
            {
                //Now check keypad keys.
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsActionKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
        }

        private string LeaveOnlyAlphaNumereicAndSpace(String inString)
        {
            String tmp = inString;
            foreach (char c in inString.ToCharArray())
            {
                if (!isAlphaNumeric(c))
                {
                    tmp = tmp.Replace(c.ToString(), "");
                }
            }
            return tmp;
        }

        public bool isAlphaNumeric(char c)
        {
            bool validFlag = false;

            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == ' '))
            {
                validFlag = true;
            }
            else
            {
                validFlag = false;
            }
            return validFlag;
        }
        #endregion

        #region Event Functions
        protected void OnKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !isAlphaNumericOrKeypad(e.Key) && !IsActionKey(e.Key);
        }

        protected void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            base.Text = LeaveOnlyAlphaNumereicAndSpace(Text);
        }
        #endregion
    }
}

1 个答案:

答案 0 :(得分:1)

有一种简单的方法可以解决您的问题,而无需更改大量代码。重置插入符号索引,如下所示textchanged事件。

 protected void OnTextChanged(object sender, TextChangedEventArgs e)
 {
         //GET YOUR CARET INDEX
         int cIndex = textbox.CaretIndex;

         textbox.Text = LeaveOnlyAlphaNumereicAndSpace(textbox.Text);

         //SET YOUR CARET INDEX
         textbox.CaretIndex = cIndex;
   }