WPF - TextBox Binding doesn't work properly - by Steve.D.Osborn

Status : 

  Duplicate<br /><br />
		This item appears to be a duplicate of another existing Connect or internal item.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 745368 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 5/31/2012 2:13:45 PM
Access Restriction Public



I have a WPF view with a TextBox, binding the Text field to a ViewModel with UpdateSourceTrigger set to PropertyChanged. In the property setter in the ViewModel, I have a simple check to prevent the text from exceeding 10 characters:

<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

public partial class MainWindow : Window
    public MainWindow()

        this.DataContext = new MainViewModel();

public string Name
    get { return _Name; }
        if (_Name != value)
            if (value.Length <= 10)
                _Name = value;
If the value isn't set, I still RaisePropertyChanged (which simply fires PropertyChanged).

The problem is that when I type in the 11th character in the UI, I don't update _Name. I fire PropertyChanged, and I can see the get accessor get called and it returns the string with only 10 characters. However, my TextBox doesn't reflect this; it still shows the string with 11 characters.
Sign in to post a comment.
Posted by Microsoft on 6/4/2012 at 5:55 PM
Previously reported as http://connect.microsoft.com/VisualStudio/feedback/details/612486/coercing-a-wpf-textbox-is-broken-in-wpf4
Posted by Microsoft on 6/1/2012 at 5:01 PM
If you look closely, you'll see that TextBox.Text agrees with your view model property - it never exceeds 10 characters. The bug is that the string the TextBox displays doesn't agree with TextBox.Text.

This is fixed in WPF 4.5, although you have to opt in to the fix by setting
FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = true;

early in the lifetime of your app - e.g. as the first line of MainWindow.ctor (or even App.ctor).
Posted by Microsoft on 5/31/2012 at 11:17 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 5/31/2012 at 2:43 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)