XAML textbox updated when TextChanged event

16,378
 <TextBox Name="textBox1"
      Height="23" Width="463"
      HorizontalAlignment="Left" 
      Margin="12,12,0,0"   
      VerticalAlignment="Top"
      Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" /> 

MSDN How to: Control When the TextBox Text Updates the Source:

The TextBox.Text property has a default UpdateSourceTrigger value of LostFocus. This means if an application has a TextBox with a data-bound TextBox.Text property, the text you type into the TextBox does not update the source until the TextBox loses focus (for instance, when you click away from the TextBox).

If you want the source to get updated as you are typing, set the UpdateSourceTrigger of the binding to PropertyChanged. In the following example, the Text properties of both the TextBox and the TextBlock are bound to the same source property. The UpdateSourceTrigger property of the TextBox binding is set to PropertyChanged.

Share:
16,378
Bastien Vandamme
Author by

Bastien Vandamme

Updated on June 07, 2022

Comments

  • Bastien Vandamme
    Bastien Vandamme almost 2 years

    I use XAML and data binding (MVVM). I need to update a Label when my user write a new text character in a TextBox.

    XAML

        <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="463" Text="{Binding OriginalText}"/>
            <Label Height="28" HorizontalAlignment="Left" Margin="12,41,0,0" Name="label1" VerticalAlignment="Top" Width="463" Content="{Binding ModifiedText}"/>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="400,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        </Grid>
    </Window>
    

    ViewModel

        class MainViewModel : NotifyPropertyChangedBase
        {
            private string _originalText = string.Empty;
            public string OriginalText
            {
                get { return _originalText; }
                set
                {
                    _originalText = value;
                    NotifyPropertyChanged("OriginalText");
                    NotifyPropertyChanged("ModifiedText");
                }
            }
    
            public string ModifiedText
            {
                get { return _originalText.ToUpper(); }
            }
        }
    

    I added a button in the XAML. The button do nothing but help me to lose the focus of my textbox. When I lose the focus the binding is updated and the upper text appears in my label. But the data binding is only ever updated when the text loses focus. The TextChanged event doesn't update the binding. I would like to force the update on the TextChanged event. How can I do that? What component should I use?