如何在WPF中设置TextBox与MVVM绑定时的TextBox.Text值
我正在使用WPF表单,我想知道如何通过TextBox与MVVM绑定来设置如何在WPF中设置TextBox与MVVM绑定时的TextBox.Text值,wpf,xaml,Wpf,Xaml,我正在使用WPF表单,我想知道如何通过TextBox与MVVM绑定来设置TextBox.Text值。 例如:TextBox.Text=“Hello”我想将此值设置为文本框和我的文本框,如 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); txtCityName.Text = "Hello Vaibhav";
TextBox.Text
值。
例如:TextBox.Text=“Hello”代码>我想将此值设置为文本框和我的文本框,如
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
txtCityName.Text = "Hello Vaibhav";
this.DataContext = new MyTesting();
}
}
我的WPF窗口窗体类:
接下来是我的Xaml:
<Grid>
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName, UpdateSourceTrigger=PropertyChanged}" Height="40" Width="200"/>
</Grid>
接下来是我的模型:
internal class MyTesting : INotifyPropertyChanged
{
private string _CityName;
public MyTesting()
{
}
public string CityName
{
get { return _CityName; }
set { _CityName = value; OnPropertyChanged("CityName"); }
}
#region PropertyChangedEventHandler
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region " RaisePropertyChanged Function "
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
#endregion
}
内部类MyTesting:INotifyPropertyChanged
{
私有字符串_CityName;
公共测试()
{
}
公共字符串城市名称
{
获取{return\u CityName;}
设置{u CityName=value;OnPropertyChanged(“CityName”);}
}
#区域属性ChangedEventHandler
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
#区域“RaisePropertyChanged函数”
///
///
///
///
私有void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
#端区
}
但为TextBox指定了NULL。如何解决这个问题试试这个:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
并且在您的代码背后:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
Xaml:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
试试这个:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
并且在您的代码背后:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
Xaml:
class AddressModel: INotifyPropertyChanged
{
private string _cityName;
public string CityName
{
get {
return _cityName;
}
set {
_cityName = value;
OnPropertyChanged("CityName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
AddressModel addressModel = new AddressModel();
addressModel.CityName = "YourCity";
this.dataContext = addressModel;
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">
txtCityName.Text=“YourCity”
不是MVVM
您应该设置CityName
源属性,而不是设置TextBox
控件的Text
属性
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName}">
this.DataContext=newaddressmodel();
AddressModel obj=this.DataContext作为AddressModel;
obj.CityName=“…”// txtCityName.Text=“YourCity”
不是MVVM
您应该设置CityName
源属性,而不是设置TextBox
控件的Text
属性
<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName}">
this.DataContext=newaddressmodel();
AddressModel obj=this.DataContext作为AddressModel;
obj.CityName=“…”//检查VS输出窗口中的绑定异常。确保设置了DataContext。目前提供的代码不足以重现问题,请创建一个最小的完整示例是否设置了datacontextthis.DataContext=new AddressModel();对我在Constructortry Text=“{Binding CityName}”的VS输出窗口中设置了DataContext,以检查绑定异常。确保设置了DataContext。目前提供的代码不足以重现问题,请创建一个最小的完整示例是否设置了datacontextthis.DataContext=new AddressModel();对我在Constructortry Text=“{Binding CityName}”中设置了DataContext,我可以这样做,但我不想这样设置,我想这样设置txtCityName.Text=“YourCity”。如何使用txtCityName实现此更改文本框值。Text=“YourCity”将更改dataContext CityName属性值。那么问题是什么?问题是它不起作用。为TextBoxYes分配的值为null。上面是我给你的密码。我想设置两种类型的文本框值,一种是我可以从MVVM设置的,另一种是直接赋值的,比如txtCityName.Text=“YourCity”。我想实现的东西//我的构造函数。public UcCountrySetup(int CountryCode,bool isCountryCode){string codeValue=Convert.ToString(CountryCode);this.DataContext=new AddressViewModel(model);BindFieldData(codeValue);}}}我可以这样做,但我不想这样设置,我想设置txtCityName.Text=“YourCity”是这样的。我如何使用txtCityName.Text=“YourCity”实现这个不断变化的文本框值“将更改dataContext CityName属性值。那么问题是什么?问题是它不起作用。为TextBoxYes分配的值为null。上面是我给你的密码。我想设置两种类型的文本框值,一种是我可以从MVVM设置的,另一种是直接赋值的,比如txtCityName.Text=“YourCity”。我想实现的东西//我的构造函数。public UcCountrySetup(int CountryCode,bool isCountryCode){string codeValue=Convert.ToString(CountryCode);this.DataContext=new AddressViewModel(model);BindFieldData(codeValue);}}这是我唯一无法设置的问题,我只想设置为txtCityName.Text=“…”我需要一个视图模型。这两种情况是不同的,在使用视图模型或使用.text时,文本框应同时设置这两种情况。我用完整的代码更新了我的问题,请查看并尝试给我解决方案。我已经告诉过您需要设置源属性的值。您是否检查了我的给定代码请使用完整的代码并检查接下来会发生什么要使用TextBox设置值。Text属性这是我唯一无法设置的问题,我只能像txtCityName一样设置。Text=“…”我需要一个视图模型。这两种情况是不同的,在使用视图模型或使用.text时,文本框应同时设置这两种情况。我用完整的代码更新了我的问题,请查看并尝试给我解决方案。我已经告诉过您需要设置源属性的值。您是否检查了我的给定代码请使用完整的代码并检查接下来会发生什么要使用TextBox.Text属性设置值吗