Textbox 控件绑定数据类型为 doubie类型时,如果触发条件为UpdateSourceTrigger=PropertyChanged时,无法输入小数点。
解决方案:
方法一:关闭数据一至检查
System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
方法二:写转换器
public class StringToDoubleConverter : IValueConverter { private static string backValue; private static bool backBool; /// <summary> /// 转换值。 /// </summary> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var str = value?.ToString(); if (string.IsNullOrEmpty(str)) return 0d; var dou = str.ToNuDouble(); if (dou is null) { return 0d; } else { if (!backBool && backValue is not null) { var l = str.Last().ToString(); var newstr = backValue + l; backValue = null; backBool = false; return newstr.ToDouble(); } if (backBool) { backBool = false; } } return dou.Value; } /// <summary> /// 转换值。 /// </summary> public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var str = value?.ToString(); if (string.IsNullOrEmpty(str)) return 0d; var dou = str.ToNuDouble(); if (dou is null) { return 0d; } else { if (str.Contains(".")) { backValue = str; backBool = true; } } return dou.Value; } }
标签:控件,PropertyChanged,return,object,backBool,UpdateSourceTrigger,str,var,dou From: https://www.cnblogs.com/zhaoxf-nx-pm-csharp-vb-381160500/p/16623569.html