2017-08-10 113 views
-1

我正在使用WPF窗體,我想知道如何設置TextBox.Text值雖然TextBox綁定與MVVM。 例如:TextBox.Text = "Hello";我想將該值設置爲文本框和喜歡如何設置TextBox.Text值,而WPF中的MVVM與TextBox綁定

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     txtCityName.Text = "Hello Vaibhav"; 
     this.DataContext = new MyTesting(); 

    } 
} 

我的WPF窗口Form類我的文本框:

下一頁我的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 
} 

但分配給TextBox的NULL。如何解決這個

+0

檢查VS結合例外輸出窗口。確保你設置了DataContext。現在提供的代碼不足以重現該問題,請創建一個最小完整示例 – ASh

+0

是否設置了datacontext – tabby

+0

this.DataContext = new AddressModel(); –

回答

0

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 
+0

這是唯一的問題,我無法像這樣設置我只能設置像txtCityName.Text =「 ..「 –

+0

然後你根本不需要視圖模型... – mm8

+0

我需要一個視圖模型。這兩種情況是不同的文本框應該在使用視圖模型或使用.Text時同時設置。 –

2

試試這個:

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}"> 
+0

我可以做到這一點,但我不想像這樣設置我想設置txtCityName.Text =「YourCity」這樣。我怎樣才能實現這個 –

+0

使用txtCityName.Text =「YourCity」更改TextBox值將更改您的dataContext CityName屬性值。所以有什麼問題? – tabby

+0

問題是,它不工作。分配給文本框的值爲空 –