使用PlaceholderText时,UWP TextBox.Text绑定不会更新

Str*_*ine 4 c# xaml textbox uwp

我有一些文本框,我想设置PlaceholderText属性.每个框的文本绑定到基础视图模型的属性.现在在XAML中设置占位符就像那样

<TextBox PlaceholderText="Placeholder" Text={Binding PropertyName} />
Run Code Online (Sandbox Code Playgroud)

我注意到,当文本框失去焦点时,视图模型的属性不再更新.而没有占位符,绑定工作正常.

这种行为是否有意,是否有任何变通方法,或者我是否必须坚持使用TextBlock描述每个框的预期输入的经典?

编辑:INotifyPropertyChanged当没有设置占位符时,属性确实实现并且在视图模型中更新绑定.

Jac*_*kie 6

TextBox的PlaceholderText在失去焦点时不会更改TextBox行为.

您可以尝试为Text属性显式使用"TwoWay"绑定模式,而不是"默认"绑定模式.

<TextBox PlaceholderText="Placeholder" Text="{x:Bind PropertyName, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

确保将View的DataContext设置为viewmodel,如下所示

    public MainPage()
    {
        this.DataContext = new MainViewModel();

        this.InitializeComponent();            
    }
Run Code Online (Sandbox Code Playgroud)

有关绑定模式的更多信息,请参阅

https://msdn.microsoft.com/en-us/library/windows/apps/mt204783.aspx