Esempio n. 1
0
 public bool ResetValue()
 {
     if (IsResetable)
     {
         object[]             owners     = this.PropertyOwners;
         PropertyDescriptor[] properties = PropertyDescriptors;
         for (int i = 0; i < owners.Length; i++)
         {
             properties[i].ResetValue(owners[i]);
             if (IsValueType(this.ParentEntry))
             {
                 string error = null;
                 if (!ParentEntry.SetValueCore(owners[i], out error) && error != null)
                 {
                     property_grid.ShowError(error);
                 }
             }
         }
         property_grid.OnPropertyValueChangedInternal(this, this.Value);
         return(true);
     }
     return(false);
 }
Esempio n. 2
0
        protected virtual bool SetValueCore(object value, out string error)
        {
            error = null;

            TypeConverter converter = GetConverter();
            Type          valueType = value != null?value.GetType() : null;

            // if the new value is not of the same type try to convert it
            if (valueType != null && this.PropertyDescriptor.PropertyType != null &&
                !this.PropertyDescriptor.PropertyType.IsAssignableFrom(valueType))
            {
                bool conversionError = false;
                try {
                    if (converter != null &&
                        converter.CanConvertFrom((ITypeDescriptorContext)this, valueType))
                    {
                        value = converter.ConvertFrom((ITypeDescriptorContext)this,
                                                      CultureInfo.CurrentCulture, value);
                    }
                } catch (Exception e) {
                    error           = e.Message;
                    conversionError = true;
                }
                if (conversionError)
                {
                    string valueText             = ConvertToString(value);
                    string errorShortDescription = null;
                    if (valueText != null)
                    {
                        errorShortDescription = "Property value '" + valueText + "' of '" +
                                                PropertyDescriptor.Name + "' is not convertible to type '" +
                                                this.PropertyDescriptor.PropertyType.Name + "'";
                    }
                    else
                    {
                        errorShortDescription = "Property value of '" +
                                                PropertyDescriptor.Name + "' is not convertible to type '" +
                                                this.PropertyDescriptor.PropertyType.Name + "'";
                    }
                    error = errorShortDescription + Environment.NewLine + Environment.NewLine + error;
                    return(false);
                }
            }

            bool changed         = false;
            bool current_changed = false;

            object[]             propertyOwners = this.PropertyOwners;
            PropertyDescriptor[] properties     = PropertyDescriptors;
            for (int i = 0; i < propertyOwners.Length; i++)
            {
                object currentVal = properties[i].GetValue(propertyOwners[i]);
                current_changed = false;
                if (!Object.Equals(currentVal, value))
                {
                    if (this.ShouldCreateParentInstance)
                    {
                        Hashtable updatedParentProperties             = new Hashtable();
                        PropertyDescriptorCollection parentProperties = TypeDescriptor.GetProperties(propertyOwners[i]);
                        foreach (PropertyDescriptor property in parentProperties)
                        {
                            if (property.Name == properties[i].Name)
                            {
                                updatedParentProperties[property.Name] = value;
                            }
                            else
                            {
                                updatedParentProperties[property.Name] = property.GetValue(propertyOwners[i]);
                            }
                        }
                        object updatedParentValue = this.ParentEntry.PropertyDescriptor.Converter.CreateInstance(
                            (ITypeDescriptorContext)this, updatedParentProperties);
                        if (updatedParentValue != null)
                        {
                            current_changed = this.ParentEntry.SetValueCore(updatedParentValue, out error);
                        }
                    }
                    else
                    {
                        try {
                            properties[i].SetValue(propertyOwners[i], value);
                        } catch {
                            // MS seems to swallow this
                            //
                            // string valueText = ConvertToString (value);
                            // if (valueText != null)
                            //  error = "Property value '" + valueText + "' of '" + properties[i].Name + "' is invalid.";
                            // else
                            //  error = "Property value of '" + properties[i].Name + "' is invalid.";
                            return(false);
                        }

                        if (IsValueType(this.ParentEntry))
                        {
                            current_changed = ParentEntry.SetValueCore(propertyOwners[i], out error);
                        }
                        else
                        {
                            current_changed = true;
                        }
                    }
                }
                if (current_changed)
                {
                    changed = true;
                }
            }
            return(changed);
        }
Esempio n. 3
0
        protected virtual bool SetValueCore(object value, out string error)
        {
            error = null;
            bool changed         = false;
            bool current_changed = false;

            object[]             propertyOwners = this.PropertyOwners;
            PropertyDescriptor[] properties     = PropertyDescriptors;
            for (int i = 0; i < propertyOwners.Length; i++)
            {
                object currentVal = properties[i].GetValue(propertyOwners[i]);
                current_changed = false;
                if (!Object.Equals(currentVal, value))
                {
                    if (this.ShouldCreateParentInstance)
                    {
                        Hashtable updatedParentProperties             = new Hashtable();
                        PropertyDescriptorCollection parentProperties = TypeDescriptor.GetProperties(propertyOwners[i]);
                        foreach (PropertyDescriptor property in parentProperties)
                        {
                            if (property.Name == properties[i].Name)
                            {
                                updatedParentProperties[property.Name] = value;
                            }
                            else
                            {
                                updatedParentProperties[property.Name] = property.GetValue(propertyOwners[i]);
                            }
                        }
                        object updatedParentValue = this.ParentEntry.PropertyDescriptor.Converter.CreateInstance(
                            (ITypeDescriptorContext)this, updatedParentProperties);
                        if (updatedParentValue != null)
                        {
                            current_changed = this.ParentEntry.SetValueCore(updatedParentValue, out error);
                        }
                    }
                    else
                    {
                        try {
                            properties[i].SetValue(propertyOwners[i], value);
                        } catch {
                            // MS seems to swallow this
                            //
                            // string valueText = ConvertToString (value);
                            // if (valueText != null)
                            //  error = "Property value '" + valueText + "' of '" + properties[i].Name + "' is invalid.";
                            // else
                            //  error = "Property value of '" + properties[i].Name + "' is invalid.";
                            return(false);
                        }

                        if (IsValueType(this.ParentEntry))
                        {
                            current_changed = ParentEntry.SetValueCore(propertyOwners[i], out error);
                        }
                        else
                        {
                            current_changed = Object.Equals(properties[i].GetValue(propertyOwners[i]), value);
                        }
                    }
                }
                if (current_changed)
                {
                    changed = true;
                }
            }
            return(changed);
        }