3

I am a beginner to use MVVM in WPF and found that it seem impossible to change the value of a textbox or a label. Here is an example.

In Xaml:

The original value of Name is "Peter".

But after I press a button which invoke a command in the ViewModel and change the value of Name to be "John". So, suppose the value of the text box will be changed to John as well. However, it doesn't change.

I have found a lot of example in the net and found that none of them implemented this kind of functions. What I have learnt from them is to use Command and ItemsSource of ListView. The value of ListView will change when I use button to raise command to change the ItemsSource of the view. Its value will change automatically when the Binding to ItemsSource changed.

However, I cannot make the value of TextBox or Label change even the value of the bindings to them are changed already.

Actually, I am really quite young in MVVM. I think I still have so much that I don't know. Could you give me an example of how exactly I should do to make change to textbox after a button click? By the way, I am not quite sure how to make command for button. It seem to involve so much codes that I found in the sample from the net. Is there any simplier way?

Thank you very much.

6
  • 2
    Does your ViewModel implement INotifyPropertyChanged? Commented Aug 11, 2011 at 8:46
  • +1 Stephan I recommend you write that up as it is probably the answer. Commented Aug 11, 2011 at 8:50
  • Thank you so much! I really didn't do so. Commented Aug 11, 2011 at 8:51
  • But how to do it? I don't find any in the net. Thanks again! Commented Aug 11, 2011 at 8:52
  • I agree with Steph, however can you put you view model code here please? Commented Aug 11, 2011 at 8:52

2 Answers 2

3

Your ViewModel needs to implement INotifyPropertyChanged . Documentation see here

public class Bar : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private string foo;
  public string Foo 
  {
    get { return this.foo; }
    set 
    { 
      if(value==this.foo) 
        return;
      this.foo = value;
      this.OnPropertyChanged("Foo");
    }
  }
  private void OnPropertyChanged(string propertyName)
  {
    if(this.PropertyChanged!=null)
      this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
  }  
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Guys, I already include the namespace System.ComponentModel, but the key word PropertyChanged is still in black color. Why?
ProertyChanged is the name of the event that is defined in INotifyPropertyChanged. You need to implement that interface. I updated my answer and added the declaration of the event.
Do I have to add anything to PropertyChanged ??
The class Bar represents your ViewModel and the property Foo represents your ViewModel's property Name.
1

Your view model should implement INotifyPropertyChanged so that WPF knows that you've altered a value of a property.

Here is an example from

// This is a simple customer class that 
// implements the IPropertyChange interface.
public class DemoCustomer  : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private string customerNameValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        var listeners = PropertyChanged;
        if (listeners  != null) 
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public string CustomerName
    {
        get
        {
            return this.customerNameValue;
        }

        set
        {
            if (value != this.customerNameValue)
            {
                this.customerNameValue = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }
}

1 Comment

Add this code to the ViewModel? Do I have to add anything to PropertyChanged? any more needed to do to make it works ar?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.