예제 #1
0
        /// <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 BindingSettings;

            // 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 TypeId 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.
                ((BindingSettings)this.GetValue(component)).TypeId = stringValue;
            }
            else if (settingsValue != null)
            {
                // Someone is explicitly setting the entire binding value, so we have to serialize it straight.
                SaveSettings(component, settings);

                // 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;
            }
        }
예제 #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;

                // 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);
        }
        public void WhenPropertyAdded_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var raised = false;
            binding.PropertyChanged += (sender, args) => raised = true;

            binding.Properties.Add(new PropertyBindingSettings());

            Assert.True(raised);
        }
        public void WhenTypeIdChanged_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var raised = false;
            binding.PropertyChanged += (sender, args) => raised = true;

            binding.TypeId = "foo";

            Assert.True(raised);
        }
예제 #5
0
 private void SaveSettings(object component, BindingSettings settings)
 {
     if (!this.settings.IsConfigured())
     {
         base.SetValue(component, BindingSettings.Empty);
     }
     else
     {
         base.SetValue(component, BindingSerializer.Serialize(settings));
     }
 }
            public void InitializeContext()
            {
                this.settings = new BindingSettings { TypeId = string.Empty };
                this.component = "SomeComponent";

                var innerDescriptor = new Mock<PropertyDescriptor>("foo", new Attribute[0]);
                innerDescriptor.Setup(x => x.Name).Returns("Property");
                innerDescriptor.Setup(x => x.Attributes).Returns(new AttributeCollection());
                innerDescriptor.Setup(x => x.GetValue(this.component)).Returns(this.settings);
                this.descriptor = new BindingPropertyDescriptor<IValueProvider>(innerDescriptor.Object, new Attribute[0]);
            }
        public void WhenPropertyValueProviderSet_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var property = new PropertyBindingSettings();
            var raised = false;
            binding.Properties.Add(property);

            binding.PropertyChanged += (sender, args) => raised = true;

            property.ValueProvider = new ValueProviderBindingSettings();

            Assert.True(raised);
        }
        public void WhenPropertyNameChanged_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var property = new PropertyBindingSettings();
            var raised = false;
            binding.Properties.Add(property);

            binding.PropertyChanged += (sender, args) => raised = true;

            property.Name = "hello";

            Assert.True(raised);
        }
        public void WhenRoundTrippingFullBinding_ThenSucceeds()
        {
            IBindingSettings binding = new BindingSettings
            {
                TypeId = "foo",
                Properties = 
                {
                    new PropertyBindingSettings
                    {
                        Name = "Name", 
                        Value = "Value", 
                        ValueProvider = new ValueProviderBindingSettings
                        {
                            TypeId = "ValueProvider",
                            Properties = 
                            {
                                new PropertyBindingSettings
                                {
                                    Name = "Id", 
                                    Value = "1"
                                }
                            }
                        }
                    }
                }
            };

            var serialized = BindingSerializer.Serialize(binding);
            var deserialized = BindingSerializer.Deserialize<IBindingSettings>(serialized);

            Assert.Equal(binding.TypeId, deserialized.TypeId);
            Assert.Equal(binding.Properties.Count, deserialized.Properties.Count);
            Assert.Equal(binding.Properties[0].Name, deserialized.Properties[0].Name);
            Assert.Equal(binding.Properties[0].Value, deserialized.Properties[0].Value);
            Assert.Equal(binding.Properties[0].ValueProvider.TypeId, deserialized.Properties[0].ValueProvider.TypeId);
            Assert.Equal(binding.Properties[0].ValueProvider.Properties[0].Name, deserialized.Properties[0].ValueProvider.Properties[0].Name);
            Assert.Equal(binding.Properties[0].ValueProvider.Properties[0].Value, deserialized.Properties[0].ValueProvider.Properties[0].Value);
        }
예제 #10
0
            public void WhenPropertyBindingHasValue_ThenBindingResolvesToFixedValue()
            {
                var settings = new BindingSettings
                {
                    TypeId = "Foo",
                    Properties =  
                    {
                        new PropertyBindingSettings
                        {
                            Name = Reflector<IFoo>.GetProperty(x => x.Message).Name, 
                            Value = FixedValue, 
                            ValueProvider = new ValueProviderBindingSettings
                            {
                                TypeId = "ValueProvider"
                            }
                        }
                    }
                };

                var binding = this.target.CreateBinding<IFoo>(settings);

                Assert.True(binding.Evaluate());
                Assert.Equal(FixedValue, binding.Value.Message);
            }
        public void WhenValueProviderTypeIdChanged_ThenBindingRaisesChanged()
        {
            var raised = false;
            var provider = new ValueProviderBindingSettings();
            var binding = new BindingSettings
            {
                Properties = 
                {
                    new PropertyBindingSettings
                    {
                        ValueProvider = provider,
                    }
                }
            };

            binding.PropertyChanged += (sender, args) => raised = true;

            provider.TypeId = "foo";

            Assert.True(raised);
        }