user1294510
user1294510

Reputation: 429

WPF xaml binding properties on textbox cannot clear

I have a textbox binded to one of my viewModel's properties

        <TextBox x:Name="box" Height="20" TextWrapping="Wrap" Text="{Binding name}"/>

viewModel.cs:

    public string name { get; set; }
    [...]
    public void clear(){
     name = "";
    }

AddCommand: Icommand class:

  public void Execute(object parameter){
  //do some stuff
  viewModel.clear();

  }

Everything else works perfect. I can read the textbox's and use them to do some calculation in viewModel then bind those calculation to labels to display. But I just cant clear those textbox after I read them. I tried setting the binding to mode=twoway but still doesn't work

Upvotes: 1

Views: 483

Answers (1)

Steve
Steve

Reputation: 11963

You need to tell WPF that the property has changed.

something similar to

private string _name;
public string name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
        PropertyChanged(this, new PropertyChangedEventArgs("name"));
    }
}

ofcourse most people would make a base class to avoid having to call that property changed method with so that complicated parameter.

Upvotes: 1

Related Questions