A place for spare thoughts


Binding to WPF TextBox seems to be broken in .NET 4.5 Beta

Filed under: wpf — Ivan Danilov @ 15:18

Recently I found that update from .NET 4.0 to .NET 4.5 Beta brings one nasty bug with it to the WPF. As Scott mentioned, .NET 4.5 is in-place upgrade to .NET 4.0, thus now I experience the same problem in any app targeted at .NET 4.0 as well.

OK, let’s go closer to the actual problem.

Create new WPF application, in MainWindow.xaml put the following:

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <TextBox Text="{Binding Path=Prop, UpdateSourceTrigger=PropertyChanged}" />

And here is MainWindow.xaml.cs:

using System.Windows.Controls;

namespace WpfApplication1
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        public decimal Prop { get; set; }

That’s all. Now, run the application. You will have TextBox stretched to the entire window with ‘0’ inside. Try to enter decimal dot (or whatever you have on your current culture for separating integer part from fractional). Nothing happens.

Now go to project properties and switch target framework to .NET 3.5 (you have to delete some references that do not exist in that framework, but I didn’t use them here anyway, so it is OK). Run the app – everything will work fine, you can enter decimal dot.

I suppose the problem is somewhere in property changing/coercion mechanism: WPF tries to parse value ‘0.’ as decimal, it succeeds and TextBox’s contents changed back to ‘0’ which is decimal value converted to string. It is also indirectly proven by the fact that if you have ‘0’ and press ‘1’ – you will have ‘1’ instead of ’01’ which one could expect. Similarly, you can’t put ‘+’ sign before number, but can put ‘-‘ sign before non-zero number. And you can’t put any sign before zero. And even more vivid proof – if you have any invalid value (i.e. that can’t be parsed as decimal) – you can do any edits you like and they are handled correctly.

Here is feedback at MS connect site about the problem.

UPD: At connect MS replied that 4.0 behavior was actually a bug and they fixed it in 4.5. In fact the fix is worse from usability point than bug was as it seems to me… see the discussion at MS Connect site (link above) for details.



  1. Microsoft fixes bugs.

    the solution is to set
    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;

    Comment by Campl3r — 04/09/2014 @ 15:36

    • Yeah, I saw answer on MS Connect. But I still think that such fix is much worse than the problem was, and that false should be the default here.

      Comment by Ivan Danilov — 04/09/2014 @ 21:24

  2. The solution would be quite simple. If update source Trigger is set to property changed, never update the text in the TextBox, EXCEPT on lost Focus. The read the source and Format it. I made my own class for this.

    public class DoubleTextBox: TextBox
    public DoubleTextBox()
    TextChanged += DoubleTextBox_TextChanged;
    LostFocus += DoubleTextBox_LostFocus;

    void DoubleTextBox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
    Text = Number.ToString(“N2”);

    void DoubleTextBox_TextChanged(object sender, TextChangedEventArgs e)
    double zahl;
    if (string.IsNullOrWhiteSpace(Text))
    Number = 0;
    else if (double.TryParse(Text, out zahl))
    Number = Double.Parse(zahl.ToString(“N2”));
    ValidationError validationError =
    new ValidationError(new ExceptionValidationRule(), GetBindingExpression(NumberProperty));

    validationError.ErrorContent = “Keine gültige Zahl”;



    public double Number
    get { return (double)this.GetValue(NumberProperty); }
    set { this.SetValue(NumberProperty, value); }

    public static readonly DependencyProperty NumberProperty = DependencyProperty.Register(
    “Number”, typeof(double), typeof(DoubleTextBox),
    new FrameworkPropertyMetadata

    Comment by Paul Steiner — 01/11/2014 @ 23:27

    • Still changes value without user’s consent. Bad UX, imo.

      Comment by Ivan Danilov — 01/11/2014 @ 23:49

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: