protected override T?ConvertTextToValue(string text) { T?result = null; if (String.IsNullOrEmpty(text)) { return(result); } // Since the conversion from Value to text using a FormartString may not be parsable, // we verify that the already existing text is not the exact same value. string currentValueText = ConvertValueToText(); if (object.Equals(currentValueText, text)) { return(this.Value); } //Don't know why someone would format a T as %, but just in case they do. result = FormatString.Contains("P") ? _fromDecimal(ParsePercent(text, CultureInfo)) : _fromText(text, this.ParsingNumberStyle, CultureInfo); if (this.ClipValueToMinMax) { return(this.GetClippedMinMaxValue()); } ValidateDefaultMinMax(result); return(result); }
/// <summary> /// Converts the value to formatted text. /// </summary> /// <returns></returns> private string ConvertValueToText() { //Manage FormatString of type "{}{0:N2} °" (in xaml) or "{0:N2} °" in code-behind. if (FormatString.Contains("{0")) { return(string.Format(CultureInfo, FormatString, Value)); } return(Value.ToString(FormatString, CultureInfo)); }
protected override string ConvertValueToText() { if (Value == null) { return(string.Empty); } if (FormatString.Contains("{0")) { return(string.Format(CultureInfo, FormatString, Value.Value)); } return(Value.Value.ToString(FormatString, CultureInfo)); }
/// <summary> /// 指定字符串转换为指定进制的数字形式, /// 显示内容的位置就是模, /// 位数就倍数 /// </summary> /// <param name="str"></param> /// <returns></returns> public long FromString(string str) { long result = 0; int j = 0; foreach (var ch in str.ToCharArray().Reverse().ToArray()) { if (FormatString.Contains(ch)) { result += FormatString.IndexOf(ch) * ((long)Math.Pow(Length, j)); j++; } } return(result); }
protected override string ConvertValueToText() { if (Value == null) { return(string.Empty); } //Manage FormatString of type "{}{0:N2} °" (in xaml) or "{0:N2} °" in code-behind. if (FormatString.Contains("{0")) { return(string.Format(CultureInfo, FormatString, Value.Value)); } return(Value.Value.ToString(FormatString, CultureInfo)); }
private T?GetClippedMinMaxValue() { T?result = FormatString.Contains("P") ? _fromDecimal(ParsePercent(this.Text, CultureInfo)) : _fromText(this.Text, this.ParsingNumberStyle, CultureInfo); if (this.IsGreaterThan(result, this.Maximum)) { return(this.Maximum); } else if (this.IsLowerThan(result, this.Minimum)) { return(this.Minimum); } return(result); }
protected override double?ConvertTextToValue(string text) { double?result = null; if (String.IsNullOrEmpty(text)) { return(result); } try { result = FormatString.Contains("P") ? Decimal.ToDouble(ParsePercent(text, CultureInfo)) : ParseDouble(text, CultureInfo); result = CoerceValue(result); } catch { Text = ConvertValueToText(); return(Value); } return(result); }
protected override int?ConvertTextToValue(string text) { int?result = null; if (String.IsNullOrEmpty(text)) { return(result); } try { //don't know why someone would format an integer as %, but just in case they do. result = FormatString.Contains("P") ? Decimal.ToInt32(ParsePercent(text, CultureInfo)) : ParseInt(text, CultureInfo); result = CoerceValue(result); } catch { Text = ConvertValueToText(); return(Value); } return(result); }
protected override double?ConvertTextToValue(string text) { double?result = 0; if (string.IsNullOrEmpty(text)) { return(result); } var currentValueText = ConvertValueToText(); if (Equals(currentValueText, text)) { return(Value); } result = FormatString.Contains("P") ? _fromDecimal(ParsePercent(text, CultureInfo)) : _fromText(text, ParsingNumberStyle, CultureInfo); ValidateDefaultMinMax(result); return(result); }