奇怪的文本框的问题与.NET 4.5 - 没有'。“允许文本框、奇怪、问题、NET

2023-09-03 03:33:57 作者:我累了

我有一个很奇怪的问题,涉及到.NET 4.5。 今天,一个用户告诉我,他是不是能够进入浮点数到一个文本框(如2.75)。 文本框只是不接受。,这是正确的分隔符在我的文化浮点数(DE-CH)。

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").

出现这个问题后,我编译.NET 4.5(以前是4.0)。

This issue occurred after I compiled the software with .NET 4.5 (formerly it was 4.0).

我可以重现这个错误。在应用程序中的所有其他文本框都工作正常。 文本框是一个普通的WPF控件。没有华丽的用户定义的控制之类的东西。

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.

还是那句话:文本框只是不接受'。作为一个字符。它似乎完全忽略它。所有其他字符(甚至是特殊的像@)的罚款。 重新编译应用程序在.NET 4.0中解决了这个问题。

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.

在XAML的文本框为:

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" />

ProcessHours的定义:

Definition of ProcessHours:

partial class ProjectTask
{
    ...
    public double TotalProcessHours { get { return ProjectBookings.Sum(b => 
b.ProcessHours); }}
    ...
}

Hours_TextChanged是:

Hours_TextChanged is:

private void Hours_TextChanged(object sender, TextChangedEventArgs e)
{
    UpdateHoursValidity();
}

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

设置文本框的语言属性特别是在XAML(语言= DE-CH)

Setting the Language property of the textbox specifically in xaml (Language=de-CH)

设置根据这些提示的文化: how设置为整个C#应用程序

Setting the culture according to these tips: how to set default culture info for entire c# application

设置根据该博文的文化: http://www.west-wind.com/weblog/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting

Setting the culture according to this blogpost: http://www.west-wind.com/weblog/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting

当我试图进入有上debugconsole没有消息了。

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

在这个任何想法?

在此先感谢!

推荐答案

这是关于文本框控件和数据相当知名的(和记录)问题的约束浮动值。你可以解决这个问题,通过增加一个的StringFormat 绑定

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" />

请调整到适合你的情况的格式。你可以找到更多的格式在自定义数字格式字符串后在MSDN。

Please adjust the format to suit your situation. You can find more formats in the Custom Numeric Format Strings post at MSDN.