【发布时间】:2015-06-13 00:20:25
【问题描述】:
我有一个文本框,我在其中处理其文本更改事件。现在,当我单击按钮时,我想从文本框中清除文本。
现在当我在文本框中有文本并且当我调用我的命令时,文本不会被清除。
xaml
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="mytxtBox">
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=SearchCommand}" CommandParameter="{Binding ElementName=mytxtBox, Path=Text}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
视图模型
public string SearchText
{
get
{
return TypedText;
}
set
{
TypedText=value;
if (string.IsNullOrEmpty(TypedText.ToString()))// This is called when the text is empty
{
Some Logic//
}
SetProperty(ref TypedText, value);
}
}
private void MyCommandExecuted(string text)
{
SearchText= string.Empty;
}
【问题讨论】:
-
SetProperty 的代码是什么?它会引发属性更改通知吗? ,您需要为 SearchText 提升属性更改
-
是的,它来自 Prism,它会发出通知
-
但它如何知道要为 SearchText 发出通知?如果可以,请发布代码
-
你的 searchtext 属性设置器不应该有那么多逻辑。只需检查新值并引发属性更改事件
-
将 RaisePropertyChanged("SearchText") 添加到设置器