I have a textbox and a button I want to clear the contents of textbox on button click. I am using MVVM prism.
My XAML is
You are not triggering the PropertyChanged-event with the correct property name "TextProperty" - or am I missing something? I've never used Prism. Try:
public string TextProperty
{
get
{
return selectedText;
}
set
{
SetProperty(ref selectedText, value, "TextProperty");
}
}
or better yet:
private void MyCommandExecuted(object obj)
{
SetProperty(TextProperty, string.Empty);
MessageBox.Show("Command Executed");
}
and remove the SetProperty call from the property setter.
Its because in your setter you are setting the field twice, one without firing PropertyChanged and the other with firing PropertyChanged , in the second set SetProperty
will raise PropertyChanged
only if there is a new value, but you already set the field to some value so the set through the SetProperty
will never raise PropertyChanged because you are setting it to the same value.
So in your setter you should remove:
selectedText = value;