コード例 #1
0
        /// <summary>
        /// When overridden in a derived class, gets the current value of the property on a component.
        /// </summary>
        public override object GetValue(object component)
        {
            // We cache the settings to avoid paying the serialization cost too often.
            // Also, this allows us to track changes more consistently.
            if (this.settings == null)
            {
                var json = base.GetValue(component) as string;

                // We always initialize the value to some non-null object, which
                // turns on the dropdown for type selection. If we don't, the
                // dropdown never shows up.
                if (string.IsNullOrWhiteSpace(json))
                {
                    this.settings = new BindingSettings();
                    // Save the value right after instantiation, so that
                    // subsequent GetValue gets the same instance and
                    // does not re-create from scratch.
                    SetValue(component, settings);
                }
                else
                {
                    // Deserialize always to the concrete type, as we the serializer needs to
                    // know the type of thing to create.
                    this.settings = BindingSerializer.Deserialize <BindingSettings>(json);
                }

                // Hookup property changed event, which supports nested changes as well. This
                // allows us to automatically save the serialized json on every property change.
                this.settings.PropertyChanged += OnSettingsChanged;
            }

            return(this.settings);
        }
コード例 #2
0
        /// <summary>
        /// When overridden in a derived class, gets the current value of the property on a component.
        /// </summary>
        public override object GetValue(object component)
        {
            // We cache the settings to avoid paying the serialization cost too often.
            // Also, this allows us to track changes more consistently.
            if (this.settings == null)
            {
                var json = base.GetValue(component) as string;

                if (string.IsNullOrWhiteSpace(json))
                {
                    this.settings = new PropertyBindingSettings {
                        Name = base.Name
                    };
                    // Save the value right after instantiation, so that
                    // subsequent GetValue gets the same instance and
                    // does not re-create from scratch.
                    SetValue(component, settings);
                }
                else
                {
                    // Deserialize always to the concrete type, as we the serializer needs to
                    // know the type of thing to create.
                    try
                    {
                        this.settings = BindingSerializer.Deserialize <PropertyBindingSettings>(json);
                        // Make sure the settings property name matches the actual descriptor property name.
                        if (this.settings.Name != base.Name)
                        {
                            this.settings.Name = base.Name;
                        }
                    }
                    catch (BindingSerializationException)
                    {
                        // This would happen if the value was a raw value from before we had a binding.
                        // Consider it the property value.
                        settings = new PropertyBindingSettings
                        {
                            Name  = base.Name,
                            Value = json,
                        };

                        // Persist updated value.
                        SetValue(component, settings);
                    }
                }

                // Hookup property changed event, which supports nested changes as well. This
                // allows us to automatically save the serialized json on every property change.
                this.settings.PropertyChanged += OnSettingsChanged;
            }

            // We know that we're always dealing with a concrete type implementation as it's not
            // externally set-able and we always instantiate a PropertyBindingSettings.
            return(new DesignProperty(this.settings)
            {
                Type = base.PropertyType,
                Attributes = this.AttributeArray
            });
        }
コード例 #3
0
ファイル: BoundProperty.cs プロジェクト: dbremner/nupattern
        private IPropertyBindingSettings CreateSettings()
        {
            IPropertyBindingSettings settings;

            if (string.IsNullOrEmpty(getValue()))
            {
                settings = new PropertyBindingSettings
                {
                    Name          = this.propertyName,
                    Value         = BindingSettings.Empty,
                    ValueProvider = null,
                };
            }
            else
            {
                // Be backwards compatible to ease migration.
                try
                {
                    settings = BindingSerializer.Deserialize <PropertyBindingSettings>(getValue());
                }
                catch (BindingSerializationException)
                {
                    // This would happen if the value was a raw value from before we had a binding.
                    // Consider it the property value.
                    settings = new PropertyBindingSettings
                    {
                        Name  = this.propertyName,
                        Value = this.getValue(),
                    };

                    // Persist updated value.
                    this.setValue(BindingSerializer.Serialize(settings));
                }
            }

            // Automatically serialize whenever something is changed in the binding.
            settings.PropertyChanged += OnSettingsChanged;

            return(settings);
        }