我有一个文本框和一个按钮,我想在单击按钮时清除文本框的内容。我正在使用 MVVM 棱镜。
My XAML is
<TextBox Grid.Row="0" Text="{Binding
Path=TextProperty,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="txtUserEntry2"/>
<Button Content="Select"
Command="{Binding Path=MyCommand}" />
和在我的查看模型
public string TextProperty
{
get
{
return selectedText;
}
set
{
selectedText = value;
SetProperty(ref selectedText, value);
}
}
//////.........
private void MyCommandExecuted(object obj)
{
TextProperty= string.Empty;
MessageBox.Show("Command Executed");
}
但它不会清除文本框。我缺少什么?
这是因为在您的设置器中,您在第二组中设置了该字段两次,一次不触发 PropertyChanged ,另一次触发 PropertyChangedSetProperty
将提高PropertyChanged
仅当有新值时,但您已将该字段设置为某个值,因此通过SetProperty
永远不会引发 PropertyChanged,因为您将其设置为相同的值。
所以在你的设置器中你应该删除:
selectedText = value;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)