wpf 只能输入基数的文本框


   

public class OddIntegerTextBox : TextBox
    {
        public bool isMouseEntered = false;

        public string PreviousText
        {
            get { return (string)GetValue(PreviousTextProperty); }
            set { SetValue(PreviousTextProperty, value); }
        }
        public static readonly DependencyProperty PreviousTextProperty =
            DependencyProperty.Register("PreviousText", typeof(string), typeof(OddIntegerTextBox), new PropertyMetadata(string.Empty));

        public string HistoryText
        {
            get { return (string)GetValue(HistoryTextProperty); }
            set { SetValue(HistoryTextProperty, value); }
        }
        public static readonly DependencyProperty HistoryTextProperty =
            DependencyProperty.Register("HistoryText", typeof(string), typeof(OddIntegerTextBox), new PropertyMetadata(string.Empty));

        public int? MinValue
        {
            get { return (int?)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(int?), typeof(OddIntegerTextBox));

        public int MaxValue
        {
            get { return (int)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        public static readonly DependencyProperty MaxValueProperty =
        DependencyProperty.Register("MaxValue", typeof(int), typeof(OddIntegerTextBox), new PropertyMetadata(int.MaxValue));

        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsGotFocusProperty);
        }

        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsGotFocusProperty, value);
        }

        public bool IsNeedLoseFucos
        {
            get { return (bool)GetValue(IsNeedLoseFucosvProperty); }
            set { SetValue(IsNeedLoseFucosvProperty, value); }
        }
        public static readonly DependencyProperty IsNeedLoseFucosvProperty =
            DependencyProperty.RegisterAttached("IsNeedLoseFucos", typeof(bool), typeof(OddIntegerTextBox), new PropertyMetadata(true));

        public bool IsGotFocus
        {
            get { return (bool)GetValue(IsGotFocusProperty); }
            set { SetValue(IsGotFocusProperty, value); }
        }
        public static readonly DependencyProperty IsGotFocusProperty =
            DependencyProperty.RegisterAttached("IsGotFocus", typeof(bool), typeof(OddIntegerTextBox), new PropertyMetadata(false));

        static OddIntegerTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(OddIntegerTextBox), new FrameworkPropertyMetadata(typeof(OddIntegerTextBox)));
        }

        public OddIntegerTextBox()
        {
            LostFocus += IntegerTextBox_LostFocus;
            PreviewTextInput += IntegerTextBox_PreviewTextInput;
            PreviewMouseLeftButtonDown += IntegerTextBox_PreviewMouseLeftButtonDown;
        }

        private void IntegerTextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HistoryText = Text;
            Text = string.Empty;
            Focusable = true;
            Focus();
            IsGotFocus = true;
        }

        private void IntegerTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Text = Text.Trim();
            string txt = Text + e.Text;

            if (txt.Length > 1 && txt.StartsWith('0'))
            {
                Text = Text.Substring(1);
            }
            if (!int.TryParse(txt, out int value))
            {
                if (!string.IsNullOrWhiteSpace(Text))
                {
                    var index = Text.IndexOf(e.Text);
                    if (index > -1) { Text = Text.Remove(index); }

                    SelectionStart = Text.Length + e.Text.Length;
                }
                e.Handled = true; // 阻止输入非数字字符
            }
            else if (value > MaxValue)
            {
                Text = MaxValue.ToString(); // 将文本框的值设置为最大值
                SelectionStart = Text.Length + e.Text.Length;
                e.Handled = true; // 阻止输入超出取值范围的数字
            }
            else
            {
                Text = value.ToString();
                SelectionStart = Text.Length + e.Text.Length;
                e.Handled = true;
            }
            SelectionStart = Text.Length + e.Text.Length;
        }

        private void IntegerTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            IsGotFocus = false;

            Text = Text.Trim();
            if (string.IsNullOrEmpty(Text))
            {
                if (!string.IsNullOrEmpty(HistoryText) && int.TryParse(HistoryText, out int history) && IsOddNumber(history) && history > MinValue)
                {
                    Text = HistoryText;
                }
                else if (MinValue is not null)
                {
                    Text = MinValue.Value.ToString();
                }
            }
            else
            {
                if (int.TryParse(Text, out int value))
                {
                    if (value < MinValue)
                    {
                        if (!string.IsNullOrEmpty(HistoryText) && int.TryParse(HistoryText, out int history) && history > MinValue)
                        {
                            Text = HistoryText;
                        }
                        else
                        {
                            Text = MinValue.ToString();
                        }
                    }
                    if (!IsOddNumber(value))
                    {
                        if (!string.IsNullOrEmpty(HistoryText) && int.TryParse(HistoryText, out int history) && IsOddNumber(history))
                        {
                            Text = HistoryText;
                        }
                        else
                        {
                            Text = MinValue.ToString();
                        }
                    }
                    HistoryText = Text;
                }
            }
            SelectionStart = Text.Length;
        }

        private bool IsOddNumber(int number)
        {
            return number % 2 != 0;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值