UWP TextBox.Text binding not updating when using PlaceholderText

I have text fields for which I wanted to set a property PlaceholderText. The text of each window is tied to a property of the underlying view model. Now that you set the placeholder in XAML as

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

I noticed that the properties of the view model are no longer updated when the text field loses focus. While without a placeholder, binding works just fine.

Is this behavior expected and are there any workarounds, or should I stick to the classic TextBlockone that describes the intended input of each window?

Change: . The property implements INotifyPropertyChanged, and the binding is updated in the view model when no placeholder is specified.

+4
source share
1 answer

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

You can explicitly use 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 DataContext view is set to view mode, something like below

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

        this.InitializeComponent();            
    }

For more information on snap mode, see

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

+5
source

Source: https://habr.com/ru/post/1624596/


All Articles