TextBox.Text值在设置时不更新

时间:2016-04-20 16:19:04

标签: c# wpf visual-studio-2013

问题: DecimalTextBox的值设置为""在代码行txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString();

之后

变量txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString();的值在visual studio中显示为320.5,所以当转换为字符串时,不应该将它放入DecimalTextBox而不会出错?

前端

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MegA="clr-namespace:MegA;assembly=MegA" x:Class="WpfApplication14.MainWindow"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Grid>
        <MegA:DecimalTextBox DollarPrecision="12" Height="22.864" Name="txtSickTime" Width="60.00" MaxLength="4" TabIndex="230" DecimalPrecision="2"/>
    </Grid>
</Window>

后端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace WpfApplication14
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Test newTest = new Test();
            txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString();
        }   
    }
    public class Test
    {
        public Decimal PutThisDecimalInTheBox;
        public Test()
        {
            PutThisDecimalInTheBox = 320.500m;
        }
    }
    public class DecimalTextBox : System.Windows.Controls.TextBox
    {//this is a modified textbox that takes in decimal values also modified to highlight all text on focus
        bool alreadyFocused;
        private string PreviousText;
        string _filterString;
        int _dollarPrecision;
        int _decimalPrecision;
        public string FilterString
        {
            get { return _filterString; }
            set { _filterString = value; }
        }

        public int DollarPrecision
        {
            get { return _dollarPrecision; }
            set { _dollarPrecision = value; }
        }

        public int DecimalPrecision
        {
            get { return _decimalPrecision; }
            set { _decimalPrecision = value; }
        }
        public decimal TextDecimal
        {
            get { return Convert.ToDecimal(this.Text); }
        }


        public DecimalTextBox()
        {
            TextAlignment = System.Windows.TextAlignment.Right;
            FontFamily = new System.Windows.Media.FontFamily("Courier New");
            FilterString = "-1234567890.";
            this.TextChanged += DecimalTextBox_TextChanged;//add the events
        }
        //event function that limits the input in the text box
        private void DecimalTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            this.TextChanged -= DecimalTextBox_TextChanged;//remove the event so it doesn't get called again
            //get the cursor location to fix it later
            int location = this.SelectionStart;
            //don't allow anything except the filter string
            if ((!string.IsNullOrEmpty(FilterString)))
            {
                if (this.Text.Trim().Length > 0)
                {
                    for (int i = 0; i <= this.Text.Length - 1; i++)
                    {
                        if (!(this.FilterString.Contains(this.Text.Substring(i, 1))))
                        {
                            this.Text = this.Text.Remove(i, 1);
                        }
                    }
                }
                string tempText = Text.Replace("-", "");
                string[] splitAtDecimal = tempText.Split('.');
                if (splitAtDecimal.Length > 2)
                {
                    Text = PreviousText;
                }
                else if (splitAtDecimal.Length == 2)
                {
                    if (splitAtDecimal[0].Length > DollarPrecision)//if violating dollar or decimal precision return to previoustext
                    {
                        Text = PreviousText;
                    }
                    if (splitAtDecimal[1].Length > DecimalPrecision)
                    {
                        Text = PreviousText;
                    }
                }
                //set the PreviousText=Text for comparison next time textchanged is called
                PreviousText = Text;
                //this.SelectionStart = this.Text.Length;
                this.TextChanged += DecimalTextBox_TextChanged;//add the event back
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题发生的原因是因为虽然visual studio将小数值显示为320.5,但当它转换为字符串时,它会保留这2个尾随零。

因此你的字符串将是320.500。

在这种情况下,问题的答案是删除尾随的零或为你的DecimalTextBox的DecimalPrecision添加+1,否则它将被恢复为DecimalTextBox的原始值&#34; &#34;

就我个人而言,我认为它应该显示尾随的零,如果它会在事后保留它们。