I am new to MVVM. to find out, I created a sample application to show a message in a text box when a button is clicked. In my code, the button command works correctly, but the property is not bound to a text field. How to bind a property to a text field using MVVM?
My code is similar to below.
View
<TextBox Name="MessageTextBox" Text="{Binding TestMessage}"/> <Button Content="Show" Name="button1" Command="{Binding ShowCommand}"> </Button>
View Model
MyMessage myMessage; public MainViewModel() { myMessage=new MyMessage(); }
Model
public class MyMessage: INotifyPropertyChanged { private string testMessage; public string TestMessage { get { return testMessage; } set { testMessage= value; OnPropertyChanged("TestName"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
niknowj
source share