コード例 #1
0
        /// <inheritdoc />
        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture)
        {
            if (!this.initialized)
            {
                Initialize();
            }

            var message = this.errorText.ToString();

            if (!(targetType == typeof(AngularJerk) || targetType == typeof(AngularJerk?)))
            {
                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 AngularJerk((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 AngularJerk(d, this.unit.Value));
                }
                AngularJerk result;
                if (AngularJerk.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 AngularJerk(d, this.unit.Value));
                    }
                }

                goto case Wpf.UnitInput.SymbolRequired;
            }

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

                return(text);
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var stringValue = reader.Value as string;

            return(AngularJerk.Parse(stringValue, serializer.Culture));
        }
コード例 #3
0
 protected void SetScalarValue(DependencyProperty property, AngularJerk? 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;
 }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Gu.Units.Wpf.AngularJerkExtension"/> class.
 /// </summary>
 /// <param name="value"><see cref="Gu.Units.AngularJerk"/>.</param>
 public AngularJerkExtension(AngularJerk value)
 {
     this.Value = value;
 }
コード例 #5
0
 protected virtual void OnMaxValueChanged(AngularJerk? oldValue, AngularJerk? newValue)
 {
     this.SetScalarValue(ScalarMaxValueProperty, newValue);
 }