Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP TextBox.Text binding not updating when using PlaceholderText

Tags:

c#

textbox

xaml

uwp

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.

like image 752
Streamline Avatar asked Mar 13 '23 14:03

Streamline


1 Answers

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

like image 162
Jackie Avatar answered Apr 26 '23 05:04

Jackie