Reputation: 1128
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" />
Upvotes: 1
Views: 1114
Reputation: 3803
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.
Upvotes: 2
Reputation: 3361
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
Upvotes: 0