【问题标题】:How to Set TextBox.Text Value while TextBox bind with MVVM in WPF如何在 WPF 中 TextBox 与 MVVM 绑定时设置 TextBox.Text 值
【发布时间】:2017-08-10 07:13:32
【问题描述】:

我正在使用 WPF 表单,我想知道如何通过 TextBox 与 MVVM 绑定来设置 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
}

但 NULL 分配给 TextBox 。如何解决这个问题

【问题讨论】:

  • 检查 VS 输出窗口是否存在绑定异常。确保设置 DataContext。目前提供的代码不足以重现问题,请创建一个最小的完整示例
  • 你设置了datacontext
  • this.DataContext = new AddressModel();
  • 是的。我在构造函数中设置 DataContext
  • 试试 Text="{Binding CityName}"

标签: wpf xaml


【解决方案1】:

试试这个:

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;

Xaml:

<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName,UpdateSourceTrigger=PropertyChanged}">

【讨论】:

  • 我可以这样做,但我不想这样设置我想像这样设置 txtCityName.Text = "YourCity"。我怎样才能做到这一点
  • 使用 txtCityName.Text = "YourCity" 更改 TextBox 值将更改您的 dataContext CityName 属性值。那是什么问题?
  • 问题是 .it 不起作用。分配给 TextBox 的值为 null
  • 是的。上面我给了你代码。我想设置两种类型的 TextBox 值,一种我可以从 MVVM 设置,第二种直接分配,如 txtCityName.Text =“YourCity”。这是我想要实现的目标
  • //我的构造函数。公共 UcCountrySetup(int CountryCode, bool isCountryCode) { string codeValue = Convert.ToString(CountryCode); this.DataContext = new AddressViewModel(model); BindFieldData(codeValue); } }
【解决方案2】:

txtCityName.Text = "YourCity" 不是 MVVM。

您应该设置CityName 源属性,而不是设置TextBox 控件的Text 属性。

<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName}">

this.DataContext = new AddressModel();

AddressModel obj = this.DataContext as AddressModel;
obj.CityName = "..."; //<--this will update the data-bound TextBox

【讨论】:

  • 这是我唯一不能这样设置的问题,我只想设置 txtCityName.Text = ".."
  • 我需要一个视图模型。两种场景都是不同的文本框,应在使用视图模型或使用 .Text 时同时设置。
  • 我用完整的代码更新了我的问题,请查看并尝试给我解决方案。
  • 我已经告诉你,你需要设置源属性的值。
  • 你检查了我给定的代码吗?请使用完整的代码并检查我想使用 TextBox.Text 属性设置值的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多