猿问

文本框自动分配默认值 wpf

我开始通过练习一个简单的加法程序来学习使用 mvvm 的 wpf。我的应用程序运行良好。


但是在运行应用程序时,文本框会自动分配默认值 0。


在用户提供任何输入之前,我不希望 0 。


视图.xaml:


 <TextBox Height="28" Margin="112,56,46,0"  Text ="{Binding firstargument}"   Name="textBox1" VerticalAlignment="Top" />

视图模型.cs


  private string _number1;

        public string firstargument

        {

            get { return _number1; }

            set

            {

                this._number1 = value;

                this.OnPropertyChanged("firstargument");


            }

        }

我的问题是执行后删除文本框中的值 0 吗?


编辑:


模型视图.cs


 class ViewModel : INotifyPropertyChanged

    {



        public RelayCommand AddNew { get; set; }


        private int _number1;

        public int firstargument

        {

            get { return _number1; }

            set

            {



                this._number1 = value;

                this.OnPropertyChanged("firstargument");


            }

        }


        private int _number2;


        public int secondargument

        {

            get { return _number2; }

            set

            {

                this._number2 = value;


                this.OnPropertyChanged("secondargument");

            }

        }


        private int _number3;


        public int _addedargument

        {

            get { return _number3; }

            set

            {

                _number3 = value;

                this.OnPropertyChanged("_addedargument");

            }

        }

    public  ViewModel()

    {


        AddNew = new RelayCommand(o => AddNumbers());

    }



    private void AddNumbers()

    {

//Model instance is created here.

        Number p = new Number() { number1 = this._number1, number2 = this._number2 };


        var c = p.number1 + p.number2;

        _addedargument = c;


    }


    #region INotifyPropertyChanged Members


    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)

    {

        if (PropertyChanged != null)

        {

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

    }

    #endregion


    }


浮云间
浏览 244回答 3
3回答

拉风的咖菲猫

我将模型的 int 属性包装在一个字符串属性中,因为 TextBox.Text 是一个字符串,其他任何东西都会产生转换错误。ViewModel 需要自己的字符串,而不是总是将用户的输入转换为 int,因为用户可能会清除该框,或者在键入“-1”的过程中部分输入了一个不是有效数字的值。当您收到转换错误时,WPF 绑定无法更新视图模型,因此您不知道有问题。&nbsp; &nbsp; private string firstArgument;&nbsp; &nbsp; public string FirstArgument&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.firstArgument;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.firstArgument= value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tempInt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (int.TryParse(value, out tempInt))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Model.FirstArgument = tempInt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.NotifyPropertyChanged();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }以下是我用来验证字符串是否为有效 int 的大部分代码。&nbsp; &nbsp; protected void NotifyPropertyChanged([CallerMemberName]string propertyName = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.CheckForPropertyErrors();&nbsp; &nbsp; &nbsp; &nbsp; this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));&nbsp; &nbsp; }&nbsp; &nbsp; public override void CheckForPropertyErrors()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.ValidateInt(this.FirstArgument , nameof(this.FirstArgument ));&nbsp; &nbsp; }&nbsp; &nbsp; protected void ValidateInt32(string value, string fieldName, string displayName = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int temp;&nbsp; &nbsp; &nbsp; &nbsp; if (!int.TryParse(value, out temp))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.AddError(new ValidationError(fieldName, Constraints.NotInt32, $"{displayName ?? fieldName} must be an integer"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.RemoveError(fieldName, Constraints.NotInt32);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

HUWWW

int变量的默认值为0。我想,这会帮助你&nbsp;private int? _number1;&nbsp; &nbsp; public int? firstargument&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _number1; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._number1 = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.OnPropertyChanged("firstargument");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

江户川乱折腾

原因您在运行窗口中获得 a 的原因0是您的绑定属性都是int并且0是 a 的默认值int。0of anint是 XAML 绑定系统的有效值,因此在您键入任何内容之前,您会看到所有TextBoxes 都包含 a 。0解决方案您的所有属性都应该是 astring并转换您的实际数字:private int? _number1;public string FirstArgument{&nbsp; &nbsp; get => _number1?.ToString();&nbsp; &nbsp; set&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (int.TryParse(value, out var number))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _number1 = number;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("FirstArgument");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _number1 = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}注意:因为您可能会得到一个无法转换为的文本int,所以您可以int?在输入错误号时使用它来存储空值。将您的数字转换为字符串或从字符串转换,以便它可以正确显示到您的 XAML 绑定系统中。该属性应该是字符串,不能是 an,int?因为 XAML 绑定系统不会自动将 a 转换string为 an&nbsp;int,除非您自己转换或编写新的IValueConverter.更新要实现该Add命令,只需使用字段而不是属性。private void AddNumbers(){&nbsp; &nbsp; var a = _number1?.Value ?? 0;&nbsp; &nbsp; var b = _number2?.Value ?? 0;&nbsp; &nbsp; var c = a + b;&nbsp; &nbsp; _addedargument = c;}如果要将结果存储到模型中,则存储a, b,c值。
随时随地看视频慕课网APP
我要回答