简体   繁体   English

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

[英]UWP TextBox.Text binding not updating when using PlaceholderText

I've got some text boxes for which i wanted to set the PlaceholderText property. 我有一些文本框,我想设置PlaceholderText属性。 The text of each box is bound to a property of the underlying view model. 每个框的文本绑定到基础视图模型的属性。 Now when setting the placeholder in the XAML like that 现在在XAML中设置占位符就像那样

<TextBox PlaceholderText="Placeholder" Text={Binding PropertyName} />

i noticed, that the view model's properties are not updated anymore when the text box loses focus. 我注意到,当文本框失去焦点时,视图模型的属性不再更新。 Whereas without placeholder the binding works just fine. 而没有占位符,绑定工作正常。

Is this behaviour intended and if are there any workarounds, or do i have to stick to a classic TextBlock that describes the intended input each box? 这种行为是否有意,是否有任何变通方法,或者我是否必须坚持使用描述每个框的预期输入的经典TextBlock

Edit: The property does implement INotifyPropertyChanged and the binding is updated in the view model when no placeholder is set. 编辑:该属性确实实现了INotifyPropertyChanged并且在未设置占位符时在视图模型中更新绑定。

PlaceholderText for TextBox does not change the TextBox behavior when it loses focus. TextBox的PlaceholderText在失去焦点时不会更改TextBox行为。

You can try explicitly using the "TwoWay" binding mode for the Text property, instead of the "Default" binding mode. 您可以尝试为Text属性显式使用“TwoWay”绑定模式,而不是“默认”绑定模式。

<TextBox PlaceholderText="Placeholder" Text="{x:Bind PropertyName, Mode=TwoWay}" />

Make sure your View's DataContext is set to your viewmodel, something like below 确保将View的DataContext设置为viewmodel,如下所示

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

        this.InitializeComponent();            
    }

For more information on Binding mode, see to 有关绑定模式的更多信息,请参阅

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM