Take the 2-minute tour ×

Is there any way to update the text binding after each key stroke?

My WPF custom TextBox uses the KeyUp event

private void MyTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    BindingExpression BE = GetBindingExpression(TextBox.TextProperty);
    if (BE != null)

but BindingExpression doesn't exist in Windows 8/RT/Store.

share|improve this question
There are some good answers below. Please mark one of them as your accepted solution. –  MEMark Jan 3 at 20:08

5 Answers 5

I know the question is a little older, but I ran into this problem today and found a very simple solution. Maybe it helps someone:

<TextBox Text="{Binding BindingSource, UpdateSourceTrigger=PropertyChanged}"/>

With the UpdateSourceTrigger-Property set to "PropertyChanged" the UpdateSource()-method is called every time the text changes (even if the focus is not lost).

share|improve this answer
This is Windows 8.1 only. The original question was posted for Windows 8 and the lack of the UpdateSourceTrigger or GetBindingExpression APIs was acknowledged as a (dreadful) bug and fixed. Effectively, as of 2014, this is the best/proper answer, i.e. convert to 8.1. –  Luke Puplett Apr 29 at 15:01
Oh, didn't know this is only available in Win 8.1 - Thanks for the additional information. –  RamNow May 30 at 15:46

Well, are you sure the text is binded properly? Make sure the object you binded to has the INotifyPropertyChanged interface inherited and that you are calling the public string property which a NotifyProperty() event.

So then if you have everything properly binded, you can simply set the TextBox Text and the XAML will automatically update the value.

Here is a tutorial on databinding: http://msdn.microsoft.com/en-us/library/ms752347.aspx

share|improve this answer
Of course it's bound correctly. I just want the bound property in the VM to be updated (and acted upon) after every change / key stroke, rather than having to wait until the TextBox loses focus. As I said, it's a relatively straightforward task in WPF, but Windows 8 is lacking in so many features. –  Peregrine Aug 31 '12 at 15:21
So then just set the text on the TextChanged event? –  Robert Gawdzik Aug 31 '12 at 15:26
If I wanted to write endless code behind event handlers, I wouldn't be using MVVM ... –  Peregrine Aug 31 '12 at 15:31
Endless code? It's literally 1 line: TextBox.TextChanged +=(o,e)=>{text = TextBox.Text;} –  Robert Gawdzik Aug 31 '12 at 15:44

You should use TextChanged rather than KeyUp or KeyDown

How to immediately update the source of a TextBox in WinRT with C#

share|improve this answer

There is way to solve your issue using attached properties. Check How To Make TextBox.Text Work article.

share|improve this answer

I can suggest you a way to accomplish this task, that would be easier on other versions of Silverlight (WPF and Windows Phone in primis)...

There's no BindingExpression for Windows RT/Store applications, as you can see!...

Implements this code, compatible with you MVVM pattern...

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace YourProject
    public static class TextBoxEx
        public static string GetRealTimeText(TextBox obj)
            return (string)obj.GetValue(RealTimeTextProperty);

        public static void SetRealTimeText(TextBox obj, string value)
            obj.SetValue(RealTimeTextProperty, value);

        public static readonly DependencyProperty RealTimeTextProperty = DependencyProperty.RegisterAttached("RealTimeText", typeof(string), typeof(TextBoxEx), null);

        public static bool GetIsAutoUpdate(TextBox obj)
            return (bool)obj.GetValue(IsAutoUpdateProperty);

        public static void SetIsAutoUpdate(TextBox obj, bool value)
            obj.SetValue(IsAutoUpdateProperty, value);

        public static readonly DependencyProperty IsAutoUpdateProperty =
            DependencyProperty.RegisterAttached("IsAutoUpdate", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false, OnIsAutoUpdateChanged));

        private static void OnIsAutoUpdateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            var textbox = (TextBox)sender;

            if ((bool)e.NewValue)
                textbox.TextChanged += textbox_TextChanged;
                textbox.TextChanged -= textbox_TextChanged;

        private static void textbox_TextChanged(object sender, TextChangedEventArgs e)
            var textbox = (TextBox)sender;

            textbox.SetValue(TextBoxEx.RealTimeTextProperty, textbox.Text);

...and then just use it in your XAML, with a small trick (a double binding), as shown below...

1) Declare the new "utility" class:

<common:LayoutAwarePage x:Name="pageRoot"
    DataContext="{Binding Path=DefaultViewModel, RelativeSource={RelativeSource Self}}"

2) Implementation in your controls:

<TextBox extra:TextBoxEx.IsAutoUpdate="True"
         extra:TextBoxEx.RealTimeText="{Binding Path=YourTextProperty, Mode=TwoWay}">
        <Binding Path="YourTextProperty"
                 Mode="OneWay" />

This worked fine for me, I hope can help you!

share|improve this answer

Your Answer


