Beispiel #1
0
        public object ConvertValueByPropertyType(string propertyName, object value)
        {
            String alias = _schema.NameToAlias(propertyName);

            // try to find by name if no alias is defined
            if (alias == null)
            {
                alias = propertyName;
            }

            CustomPropertyInfoHelper propertyInfo = _schema.CustomProperties.FirstOrDefault(prop => prop.Name == alias);

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

            if (ValidateValueType(value, propertyInfo._type))
            {
                return(value);
            }

            object convertedValue = value;

            if (!ValidateValueType(value, propertyInfo._type))
            {
                convertedValue = Helper.ConvertValue(value, propertyInfo.Field.Type);
            }

            return(convertedValue);
        }
Beispiel #2
0
        public bool SetPropertyValue(string propertyName, object value)
        {
            String alias = _schema.NameToAlias(propertyName);

            // try to find by name if no alias is defined
            if (alias == null)
            {
                alias = propertyName;
            }

            CustomPropertyInfoHelper propertyInfo = _schema.CustomProperties.FirstOrDefault(prop => prop.Name == alias);

            if (propertyInfo == null)
            {
                return(false);
            }

            // allow adding types on the fly
            //if (propertyInfo == null || !_customPropertyValues.ContainsKey(alias))
            //  throw new Exception("There is no property with the name " + alias);

            object convertedValue = value;

            if (!ValidateValueType(value, propertyInfo._type))
            {
                convertedValue = Helper.ConvertValue(value, propertyInfo.Field.Type);
            }

            if (!_valuesDictionary.ContainsKey(alias))
            {
                // allow adding types on the fly
                ItemAttribute newValue = new ItemAttribute(alias, convertedValue);
                _valuesDictionary.Add(alias, newValue);
                _valuesCollection.Add(newValue);
                RaisePropertyChanged(alias);
            }
            else
            {
                ItemAttribute currentValue = _valuesDictionary[alias];
                if (currentValue.Value != (object)value) //TODO - consider comparing by ToString() values instead
                {
                    currentValue.Value = convertedValue;
                    RaisePropertyChanged(alias);
                }
            }

            return(true);
        }