我开始通过练习一个简单的加法程序来学习使用 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
}
拉风的咖菲猫
HUWWW
江户川乱折腾
相关分类