Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird TextBox issues with .NET 4.5 - no '.' allowed

Tags:

c#

.net

wpf

textbox

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!

like image 723
SKull Avatar asked Aug 27 '13 13:08

SKull


2 Answers

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.

like image 173
Sheridan Avatar answered Nov 07 '22 03:11

Sheridan


There is a detailed discussion on the .NET 4.5 change that causes this issue on the Microsoft Connect site: https://connect.microsoft.com/VisualStudio/feedback/details/737301/binding-to-wpf-textbox-seems-to-be-broken-in-net-4-5-beta

In addition to the StringFormat workaround, another option is to set a Delay for the binding.

like image 31
Govert Avatar answered Nov 07 '22 04:11

Govert