示例#1
0
 /// <summary>
 /// Initializes a new instance of <see cref="Gu.Units.Wpf.MagneticFieldStrengthExtension"/>.
 /// </summary>
 /// <param name="value"><see cref="Gu.Units.MagneticFieldStrength"/>.</param>
 public MagneticFieldStrengthExtension(MagneticFieldStrength value)
 {
     this.Value = value;
 }
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var stringValue = reader.Value as string;

            return(MagneticFieldStrength.Parse(stringValue, serializer.Culture));
        }
 protected void SetScalarValue(DependencyProperty property, MagneticFieldStrength? quantity)
 {
     // we set this flag to prevent from setting scalar value changing quantity values.
     this.isUpdatingScalarValue = true;
     var value = quantity != null
         ? this.Unit.GetScalarValue(quantity.Value)
         : (double?)null;
     this.SetCurrentValue(property, value);
     this.isUpdatingScalarValue = false;
 }
        /// <inheritdoc />
        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture)
        {
            if (!this.initialized)
            {
                Initialize();
            }

            var message = this.errorText.ToString();

            if (!(targetType == typeof(MagneticFieldStrength) || targetType == typeof(MagneticFieldStrength?)))
            {
                message += $"{GetType().Name} does not support converting to {targetType.Name}";
            }

            if (message != string.Empty)
            {
                message = message.TrimEnd('\r', '\n');
                if (Is.DesignMode)
                {
                    throw new InvalidOperationException(message);
                }

                return(message);
            }

            if (value == null)
            {
                return(null);
            }


            if (value is double)
            {
                return(new MagneticFieldStrength((double)value, this.unit.Value));
            }

            var text = value as string;

            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            var unitInput = UnitInput ?? Wpf.UnitInput.ScalarOnly;

            switch (unitInput)
            {
            case Wpf.UnitInput.ScalarOnly:
            {
                double d;
                if (double.TryParse(text, NumberStyles.Float, culture, out d))
                {
                    return(new MagneticFieldStrength(d, this.unit.Value));
                }
                MagneticFieldStrength result;
                if (MagneticFieldStrength.TryParse(text, NumberStyles.Float, culture, out result))
                {
                    return($"#{text}#");        // returning modified text so that TypeConverter fails and we get an error
                }

                return(text);        // returning raw to trigger error
            }

            case Wpf.UnitInput.SymbolAllowed:
            {
                double d;
                int    pos = 0;
                WhiteSpaceReader.TryRead(text, ref pos);
                if (DoubleReader.TryRead(text, ref pos, NumberStyles.Float, culture, out d))
                {
                    WhiteSpaceReader.TryRead(text, ref pos);
                    if (pos == text.Length)
                    {
                        return(new MagneticFieldStrength(d, this.unit.Value));
                    }
                }

                goto case Wpf.UnitInput.SymbolRequired;
            }

            case Wpf.UnitInput.SymbolRequired:
            {
                MagneticFieldStrength result;
                if (MagneticFieldStrength.TryParse(text, NumberStyles.Float, culture, out result))
                {
                    return(result);
                }

                return(text);
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 protected virtual void OnMaxValueChanged(MagneticFieldStrength? oldValue, MagneticFieldStrength? newValue)
 {
     this.SetScalarValue(ScalarMaxValueProperty, newValue);
 }