我有一些文本框,我想为其设置 PlaceholderText
属性。每个框的文本都绑定(bind)到底层 View 模型的属性。现在,当像那样在 XAML 中设置占位符时
<TextBox PlaceholderText="Placeholder" Text={Binding PropertyName} />
我注意到,当文本框失去焦点时, View 模型的属性不再更新。 而在没有占位符的情况下,绑定(bind)工作正常。
这种行为是故意的吗?如果有任何解决方法,还是我必须坚持使用经典的 TextBlock
来描述每个框的预期输入?
编辑:该属性确实实现了 INotifyPropertyChanged
,并且当未设置占位符时绑定(bind)会在 View 模型中更新。
最佳答案
TextBox 的 PlaceholderText 在失去焦点时不会改变 TextBox 的行为。
您可以尝试为 Text 属性显式使用“TwoWay”绑定(bind)模式,而不是“默认”绑定(bind)模式。
<TextBox PlaceholderText="Placeholder" Text="{x:Bind PropertyName, Mode=TwoWay}" />
确保您的 View 的 DataContext 设置为您的 View 模型,如下所示
public MainPage()
{
this.DataContext = new MainViewModel();
this.InitializeComponent();
}
有关绑定(bind)模式的更多信息,请参阅
https://msdn.microsoft.com/en-us/library/windows/apps/mt204783.aspx
关于c# - 使用 PlaceholderText 时 UWP TextBox.Text 绑定(bind)不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829111/