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

2019-04-24 11:58发布

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

There is NO Message on the debugconsole when I try to enter a ".".

Any ideas on this one?

Thanks in advance!

2条回答
Anthone
2楼-- · 2019-04-24 12:35

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-24 12:38

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.

查看更多
登录 后发表回答