1

What is the difference between .Net 3.5 and 4.0 when using the StringFormat in a property binding?

I have a WPF window with just a textbox whos Text property binds to a property of type double. In 3.5 the textbox works as expected when I edit the number but in 4.0 I can only edit one side of the decimal place and using backspace or delete causes strange formatting to occur and overwriting of data. The best way to show this would be to try the example targeting different frameworks.

 <TextBox FontSize="16" Text="{Binding Path=SetPoint, UpdateSourceTrigger=PropertyChanged, StringFormat=0.000}"  Margin="185,130,209,146" />

Also I have noticed that when I removed the UpdateSourceTrigger then the .Net 4.0 works like the 3.5 version. Were the the UpdateSourceTriggers reversed in the different versions?

 <TextBox FontSize="16" Text="{Binding Path=SetPoint, StringFormat=0.000}"  Margin="185,130,209,146" />
1

2 Answers 2

2

I believe this Microsoft Connect issue describes the problem that you are seeing.

In 3.5, the binding would write a new value back to the source after each keystroke, without changing the TextBox text. But that text might not represent the source's value accurately, perhaps because it doesn't include formatting and conversion, or because the source changed the value (in the property-setter) to something else. This led to frequent and vehement complaints - people wanted the TextBox to show the source's value, exactly as a TextBlock would if bound to the same property with the same converters and formatting. The UI should display what's actually in the data, not what the end-user typed.

To fix this class of bugs in 4.0, the binding now applies formatting and conversion to the source's new value after every update. (LostFocus bindings already did this in 3.5.) The TextBox now shows what's in the data, but that can make the user's typing more complex.

1
  • Thanks! I think this is exactly what I am seeing. To me it seems it would have been better to keep it the way it was in 3.5 because the clients I see using the application get very confused by this behavior.
    – MisterXero
    Jun 29, 2012 at 14:07
0

The standard WPF is a bit different in xaml code, using:

.., StringFormat={}{0:C2}}"
.., StringFormat={}{0:dd/MM/yyyy}}"

Other samples:
http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/
http://www.codeproject.com/Articles/195436/Formatting-text-in-Silverlight-XAML-using-StringFo

4
  • Thanks for the links but I still have the same issue when switching between .Net v3.5 and 4.0. I want to know why the formatting is handled different between the two versions?
    – MisterXero
    Jun 28, 2012 at 21:41
  • Well, would have to test to find out. I believe that if you follow the correct default "StringFormat" will also work.
    – J. Lennon
    Jun 28, 2012 at 22:09
  • I did try it using the formatting found in those links but the result was still the same.
    – MisterXero
    Jun 29, 2012 at 14:07
  • To recommend to use special formatting: wpftoolkit.codeplex.com (MaskedTextBox)
    – J. Lennon
    Jun 29, 2012 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.