diff --git a/Perspex.UnitTests/PerspexObjectTests.cs b/Perspex.UnitTests/PerspexObjectTests.cs index 0f39aff767..a79973777a 100644 --- a/Perspex.UnitTests/PerspexObjectTests.cs +++ b/Perspex.UnitTests/PerspexObjectTests.cs @@ -4,6 +4,7 @@ // // ----------------------------------------------------------------------- +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Perspex.UnitTests { using System; @@ -133,6 +134,15 @@ namespace Perspex.UnitTests Assert.IsFalse(raised); } + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void SetValue_Throws_Exception_For_Unregistered_Property() + { + Class1 target = new Class1(); + + target.SetValue(Class2.BarProperty, "invalid"); + } + [TestMethod] public void GetObservable_Returns_Initial_Value() { diff --git a/Perspex/Controls/Decorator.cs b/Perspex/Controls/Decorator.cs index c2db2d1e21..36e74ea079 100644 --- a/Perspex/Controls/Decorator.cs +++ b/Perspex/Controls/Decorator.cs @@ -14,10 +14,10 @@ namespace Perspex.Controls public class Decorator : Control, IVisual { public static readonly PerspexProperty ContentProperty = - PerspexProperty.Register("Content"); + PerspexProperty.Register("Content"); public static readonly PerspexProperty PaddingProperty = - PerspexProperty.Register("Padding"); + PerspexProperty.Register("Padding"); public Decorator() { diff --git a/Perspex/Controls/TextBlock.cs b/Perspex/Controls/TextBlock.cs index ec387f93b1..b82fefe2f3 100644 --- a/Perspex/Controls/TextBlock.cs +++ b/Perspex/Controls/TextBlock.cs @@ -19,7 +19,7 @@ namespace Perspex.Controls inherits: true); public static readonly PerspexProperty TextProperty = - PerspexProperty.Register("Text"); + PerspexProperty.Register("Text"); public TextBlock() { diff --git a/Perspex/PerspexObject.cs b/Perspex/PerspexObject.cs index 442b08be8d..41179842eb 100644 --- a/Perspex/PerspexObject.cs +++ b/Perspex/PerspexObject.cs @@ -326,8 +326,8 @@ namespace Perspex /// /// Checks whether a is set on this object. /// - /// - /// + /// The property. + /// True if the property is set, otherwise false. public bool IsSet(PerspexProperty property) { Contract.Requires(property != null); @@ -335,6 +335,33 @@ namespace Perspex return this.values.ContainsKey(property); } + /// + /// Checks whether a is registered on this class. + /// + /// The property. + /// True if the property is registered, otherwise false. + private bool IsRegistered(PerspexProperty property) + { + Type type = this.GetType(); + + while (type != null) + { + List list; + + if (registered.TryGetValue(type, out list)) + { + if (list.Contains(property)) + { + return true; + } + } + + type = type.GetTypeInfo().BaseType; + } + + return false; + } + /// /// Sets a value. /// @@ -347,6 +374,14 @@ namespace Perspex const int Priority = (int)BindingPriority.LocalValue; PriorityValue v; + if (!this.IsRegistered(property)) + { + throw new InvalidOperationException(string.Format( + "Property '{0}' not registered on '{1}'", + property.Name, + this.GetType())); + } + if (!this.values.TryGetValue(property, out v)) { if (value == PerspexProperty.UnsetValue)