示例#1
0
        protected override bool ParseValue(object value)
        {
            if (TryCastBool(value, out var asBool))
            {
                if (IsValueAllowed(asBool))
                {
                    return(asBool);
                }
            }

            // given value is not allowed, return the current set value instead
            if (Value is bool currentValue && IsValueAllowed(currentValue))
            {
                return(currentValue);
            }

            var predefinedValues = PredefinedValues.ToList();

            if (predefinedValues.Count == 0)
            {
                return(!IsValueAllowed(false));
            }

            return(PredefinedValues.OfType <bool>().FirstOrDefault());
        }
示例#2
0
        protected override double ParseValue(object value)
        {
            var minMax = MinMax();
            var min    = (double)minMax.Min;
            var max    = (double)minMax.Max;

            if (TryCastNumeric <double>(value, out var asDouble))
            {
                if (IsNumericValueAllowed(asDouble))
                {
                    return(asDouble);
                }
                else if (IsValueAllowed(asDouble))
                {
                    if (asDouble > max)
                    {
                        return(max);
                    }

                    if (asDouble < min)
                    {
                        return(min);
                    }
                }
            }

            // given value is not allowed, return the current set value instead
            if (Value is double currentValue && IsNumericValueAllowed(currentValue))
            {
                return(currentValue);
            }

            var predefinedValues = PredefinedValues;

            if (predefinedValues.Count == 0)
            {
                var newValue = min;
                while (!IsNumericValueAllowed(newValue) && newValue <= max)
                {
                    newValue += 1;
                }

                return(newValue);
            }

            return(PredefinedValues.OfType <double>().FirstOrDefault());
        }
示例#3
0
        protected override string ParseValue(object value)
        {
            var asString = value as string ?? value?.ToString();

            if (IsValueAllowed(asString))
            {
                return(asString);
            }

            // given value is not allowed, return the current set value instead
            var currentValue = Value as string ?? Value?.ToString();

            // if current value is also null, try to use the first PredefinedValue
            var allowedValues = PredefinedValues.OfType <string>();

            return(IsValueAllowed(currentValue) ? currentValue : allowedValues.FirstOrDefault());
        }
        protected override T ParseValue(object value)
        {
            if (value is T asT)
            {
                if (IsValueAllowed(asT))
                {
                    if (!ReferenceEquals(Value, asT) || !SubEditables.Any())
                    {
                        CreateSubEditables(asT);
                    }
                    return(asT);
                }
                else if (Value is T currentValue && IsValueAllowed(currentValue))
                {
                    return(currentValue);
                }
            }

            // if null is allowed, return null
            var isNullAllowed = PredefinedValues.Any(x => x == null);

            if (isNullAllowed)
            {
                return(null);
            }

            var predefinedValuesOfT = PredefinedValues.OfType <T>().ToList();

            if (predefinedValuesOfT.Any())
            {
                return(predefinedValuesOfT.First());
            }

            // otherwise create a new instance
            var newInstance = (T)InstantiateObjectOfSpecificType(GetPredefinedType());

            CreateSubEditables(newInstance);
            return(newInstance);
        }
示例#5
0
        protected override KeyValuePair <TKey, TValue> ParseValue(object value)
        {
            if (value is KeyValuePair <TKey, TValue> asKeyValuePair && IsValueAllowed(asKeyValuePair))
            {
                return(asKeyValuePair);
            }

            // given value is not allowed, return the current set value instead
            if (Value is KeyValuePair <TKey, TValue> currentValue && IsValueAllowed(currentValue))
            {
                return(currentValue);
            }

            var predefinedValues = PredefinedValues.ToList();

            if (predefinedValues.Count == 0)
            {
                return(new KeyValuePair <TKey, TValue>());
            }

            return(PredefinedValues.OfType <KeyValuePair <TKey, TValue> >().FirstOrDefault());
        }