WPF – Text Binding

di Luigi Boschetti

In this blog we will illustrate some features of the Binding concepts applied to Text properties. Some of the characteristics will be appliable to other kind of bindings, not only Text Properties.


In this post, we assume you know the “Property Change Notification” technology that is the base of the WPF binding system. To focus the code on the target of the blog, we will use the CommunityToolkit.MVVM, to manage the notification process.


Typical binding for Text property is something like this:

<Textbox Text="{Binding MyTextVariable}"/>

Consider this example:


namespace BlogApp
    public class User : ObservableObject
        private string name;
        public string Name
            get =&gt; name;
            set =&gt; SetProperty(ref name, value);

        private string surname;
        public string Surname
            get =&gt; surname;
            set =&gt; SetProperty(ref surname, value);

        public User()
            Name = "Luigi";

Window XAML:

<Window x:Class="BlogApp.MainWindow"
        Title="MainWindow" Height="450" Width="800"/>
        <local:User />
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="15"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="*"/>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Name: " FontWeight="Bold"/>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name}"/>

        <TextBlock Grid.Row="1" Grid.Column="3" Text="Test: " FontWeight="Bold"/>
        <TextBlock Grid.Row="1" Grid.Column="4" Text="{Binding Name}"/>

        <TextBlock Grid.Row="2" Grid.Column="0" Text="Surname: " FontWeight="Bold"/>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Surname}"/>

        <TextBlock Grid.Row="2" Grid.Column="3" Text="Test: " FontWeight="Bold"/>
        <TextBlock Grid.Row="2" Grid.Column="4" Text="{Binding Surname}"/>

This is very simple example. The Binding part is in the third last line (in bold).

This binding works due to the NotifyPropertyChanged process of the property “Name” of the ViewModel User.

How It Works:

When the application start, the MainWindow instanciate a new User class (View Model). During the initialization, the property Name is initialized with the name “Luigi”.

Now try to change the “Name” from Luigi to your name: you will see the name change in the TextBox, but not in the Test TexBlock on its right! Now move the focus on the “Surname” Textbox and… Here the Magic! The Test for the name changed to the new value! Wonderful! But… Why it changes only when the TextBox for the name looses focus? If we would chenge it “live”?


Now try to change the Xaml code for the Name Textbox like the follow:

<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>

Then start the app, go to the Name Textbox and start type something: you will see the test for the Name change in real time!

UpdateSourceTrigger is an attribute of the Binding that manage the moment in which the the ViewModel property is read, The possibilities are:

  • Default: in other words… the dafault value for the property. According to MSDN, it is “PropertyChanged” for most of properties and “LostFocus” for “TextBox.Text“.
  • Explicit: binded ViewModel property will be update only when you call UpdateSource() method.
  • LostFocus: binded ViewModel property is updated when the TextBox we are editing loose focus.
  • PropertyChanged: binded ViewModel property is updated every time the property is changed. In our example, every time we press a key, we change the Text of the text box.