WPF DataGridTextColumn可以';浮点数据的t型点

WPF DataGridTextColumn可以';浮点数据的t型点,wpf,datagrid,datagridtextcolumn,Wpf,Datagrid,Datagridtextcolumn,我有一个WPF DataGrid,并使用DataGridTextColumn绑定到集合。集合中的项具有一些float属性 当我的程序启动时,我在DataGrid中修改了float属性的值,如果我键入一个整数值,它工作得很好。但是如果我输入char。对于浮点值,char。不能打字。我必须先把所有的数字都打出来,然后跳到最前面。输入char的位置。完成我的输入 那我怎么打字呢。在我的情况下 谢谢。这可能是本地化的问题。 尝试更改线程的区域性设置,以检查这是否是问题所在: using System.G

我有一个WPF DataGrid,并使用DataGridTextColumn绑定到集合。集合中的项具有一些float属性

当我的程序启动时,我在DataGrid中修改了float属性的值,如果我键入一个整数值,它工作得很好。但是如果我输入char。对于浮点值,char。不能打字。我必须先把所有的数字都打出来,然后跳到最前面。输入char的位置。完成我的输入

那我怎么打字呢。在我的情况下


谢谢。

这可能是本地化的问题。 尝试更改线程的区域性设置,以检查这是否是问题所在:

using System.Globalization;
using System.Threading;

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
如果您不确定在哪个区域性中运行,您可以通过转到
控制面板>时钟、语言和区域
或运行以下代码来再次检查设置:

using System.Diagnostics;

Debug.WriteLine("decimal: " + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator);
Debug.WriteLine("thousand: " + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator);

在绑定中尝试此正则表达式验证

<Validator:RegexValidationRule x:Key="DecimalValidatorFor3Digits"
    RegularExpression="^\d{0,3}(\.\d{0,2})?$"
    ErrorMessage="The field must contain only numbers with max 3 integers and 2 decimals" />

谢谢


Ck Nitin(TinTin)

我想这是因为datagridcolumn使用decimal数据类型绑定到类成员,例如

public class Product : ModelBase
{
    decimal _price = 0;
    public decimal Price
    {
        get { return _price; }
        set { _price = value; OnPropertyChanged("Price"); }
    }
}
并且UpdateSourceTrigger=PropertyChanged。一种方法是将属性更改为字符串类型,并按如下方式操作字符串:

string _price = "0.00";
    public string Price
    {
        get { return _price; }
        set 
        {
            string s = value;
            decimal d = 0;
            if (decimal.TryParse(value, out d))
                _price = s;
            else
                _price = s.Substring(0, s.Length == 0 ? 0 : s.Length - 1);
            OnPropertyChanged("Price"); 
        }
    }

希望它有帮助

也遇到了同样的问题

对于我来说,这是由于数据绑定选项

我从
*.UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged更改
*。UpdateSourceTrigger=UpdateSourceTrigger.LostFocus


然后它可以直接键入
float
number。

发布代码后,您可能有一个转换器或验证器用于int而不是float,或者可能是本地化问题。一些国家使用。(句号)作为小数分隔符,有些使用(逗号)作为分隔符。两个都试试?嗨,谢谢你回答我的问题。检查我的本地化设置后,我的电脑设置为:“十进制符号:”。设置正确。您好,谢谢您回答我的问题。我可以在绑定中使用StringFormat来解决我的问题吗?DataGridTextColumn.Binding中只能使用ValidateSondaErrors、ValidatesOnExceptions、ValidateNotifyDataErrors和StringFormat。