简体   繁体   中英

UWP TextBox.Text binding not updating when using PlaceholderText

I've got some text boxes for which i wanted to set the PlaceholderText property. 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

<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?

Edit: The property does implement INotifyPropertyChanged and the binding is updated in the view model when no placeholder is set.

PlaceholderText for TextBox does not change the TextBox behavior when it loses focus.

You can try explicitly using the "TwoWay" binding mode for the Text property, instead of the "Default" binding mode.

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

Make sure your View's DataContext is set to your viewmodel, something like below

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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