How to format number of decimal places in wpf using style/template?

后端 未结 3 608
再見小時候
再見小時候 2020-11-29 01:13

I am writing a WPF program and I am trying to figure out a way to format data in a TextBox through some repeatable method like a style or template. I have a lot of TextBoxes

3条回答
  •  独厮守ぢ
    2020-11-29 01:27

        void NumericTextBoxInput(object sender, TextCompositionEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            var regex = new Regex(@"^[0-9]*(?:\.[0-9]{0,1})?$");
            string str = txt.Text + e.Text.ToString();
            int cntPrc = 0;
            if (str.Contains('.'))
            {
                string[] tokens = str.Split('.');
                if (tokens.Count() > 0)
                {
                    string result = tokens[1];
                    char[] prc = result.ToCharArray();
                    cntPrc = prc.Count();
                }
            }
            if (regex.IsMatch(e.Text) && !(e.Text == "." && ((TextBox)sender).Text.Contains(e.Text)) && (cntPrc < 3))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    

提交回复
热议问题