/// <summary> /// When overridden in a derived class, sets the value of the component to a different value. /// </summary> public override void SetValue(object component, object value) { var stringValue = value as string; var settingsValue = value as IPropertyBindingSettings; // The setter will be called with a plain string value whenever the standard values editor is // used to pick the TypeId. So we need to actually set that value on the binding instead. if (stringValue != null) { // Note that the setting of the property Value automatically triggers a property changed // that will cause the value to be serialized and saved :). Indeed, // it will be calling the SetValue again, but this time with the entire binding settings // so it will call the next code branch. ((IPropertyBindingSettings)this.GetValue(component)).Value = stringValue; } else if (settingsValue != null) { // Someone is explicitly setting the entire binding value, so we have to serialize it straight. base.SetValue(component, BindingSerializer.Serialize(settingsValue)); // If the previous value was non-null, then we'd be monitoring its property changes // already, and we'd need to unsubscribe. if (this.settings != null) { this.settings.PropertyChanged -= OnSettingsChanged; } // Store for reuse on GetValue, and attach to property changes for auto-save. this.settings = settingsValue; this.settings.PropertyChanged += OnSettingsChanged; } }
/// <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); }
/// <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 }); }
private void SaveSettings() { if (!this.settings.IsConfigured()) { this.setValue(BindingSettings.Empty); } else { this.setValue(BindingSerializer.Serialize(this.settings)); } }
private void SaveSettings(object component, BindingSettings settings) { if (!this.settings.IsConfigured()) { base.SetValue(component, BindingSettings.Empty); } else { base.SetValue(component, BindingSerializer.Serialize(settings)); } }
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); }