Weird TextBox issues with .NET 4.5 - no '.' allowed
.net, c#, textbox, wpf
Solution
This is a fairly well known (and documented) issue relating to `TextBox` controls and data bound `float` values. You can fix this issue, by adding a `StringFormat` to your `Binding`:
<TextBox x:Name="_Hours" Grid.Row="9" Grid.Column="1" VerticalAlignment="Center"
TextAlignment="Center" FontWeight="Bold" FontSize="16" Text="{Binding ProcessHours,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={}{##.##}}"
TextChanged="Hours_TextChanged" />
Please adjust the format to suit your situation. You can find more formats in the Custom Numeric Format Strings post at MSDN.
Problem
I've got a really weird problem related to .NET 4.5. Today a User told me that he isn't able to enter floating numbers into a Textbox (like "2.75"). The textbox just doesn't accept ".", which is the correct 'seperator' for floating numbers in my Culture ("de-CH"). This issue occurred after I compiled the software with .NET 4.5 (formerly it was 4.0). I can reproduce this error. All other textboxes in the application are working fine. The textbox is a regular WPF Control. No fancy user defined control or anything like that. Again: the textbox just doesn't accept '.' as a character. It seems that it completely ignores it. Every other character (even special ones like "@") are fine. Recompiling the application on .NET 4.0 solves the problem. The xaml for the textbox is: ``` <TextBox x:Name="_Hours" Grid.Row="9" Grid.Column="1" VerticalAlignment="Center" TextAlignment="Center" FontWeight="Bold" FontSize="16" Text="{Binding ProcessHours, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="Hours_TextChanged" /> ``` Definition of ProcessHours: ``` partial class ProjectTask { ... public double TotalProcessHours { get { return ProjectBookings.Sum(b => b.ProcessHours); }} ... } ``` Hours_TextChanged is: ``` private void Hours_TextChanged(object sender, TextChangedEventArgs e) { UpdateHoursValidity(); } ``` UpdateHoursValidity() just fades a Text Message below the actual textbox. It is not connected with the "broken" textbox in any way: ``` private void UpdateHoursValidity() { string key = IsInvalidHoursWarning ? "ShowWarningStoryboard" : "HideWarningStoryboard"; var storyboard = FindResource(key) as Storyboard; if(storyboard != null) storyboard.Begin(); } ``` So nothing fancy here either. What I tried so far: - removing the textbox, recompiling, adding the textbox again, recompiling -> same situation Setting the Language property of the textbox specifically in xaml (Language=de-CH) Setting the culture according to these tips: how to set default culture info for entire c# application Setting the culture according to this blogpost: http://www.west-wind.com/weblog/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting There is NO Message on the debugconsole when I try to enter a ".". Any ideas on this one? Thanks in advance!