Browse Source

Check properties are registered when setting.

pull/4/head
Steven Kirk 12 years ago
parent
commit
1de05e4262
  1. 10
      Perspex.UnitTests/PerspexObjectTests.cs
  2. 4
      Perspex/Controls/Decorator.cs
  3. 2
      Perspex/Controls/TextBlock.cs
  4. 39
      Perspex/PerspexObject.cs

10
Perspex.UnitTests/PerspexObjectTests.cs

@ -4,6 +4,7 @@
// </copyright>
// -----------------------------------------------------------------------
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()
{

4
Perspex/Controls/Decorator.cs

@ -14,10 +14,10 @@ namespace Perspex.Controls
public class Decorator : Control, IVisual
{
public static readonly PerspexProperty<Control> ContentProperty =
PerspexProperty.Register<ContentControl, Control>("Content");
PerspexProperty.Register<Decorator, Control>("Content");
public static readonly PerspexProperty<Thickness> PaddingProperty =
PerspexProperty.Register<ContentControl, Thickness>("Padding");
PerspexProperty.Register<Decorator, Thickness>("Padding");
public Decorator()
{

2
Perspex/Controls/TextBlock.cs

@ -19,7 +19,7 @@ namespace Perspex.Controls
inherits: true);
public static readonly PerspexProperty<string> TextProperty =
PerspexProperty.Register<Border, string>("Text");
PerspexProperty.Register<TextBlock, string>("Text");
public TextBlock()
{

39
Perspex/PerspexObject.cs

@ -326,8 +326,8 @@ namespace Perspex
/// <summary>
/// Checks whether a <see cref="PerspexProperty"/> is set on this object.
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
/// <param name="property">The property.</param>
/// <returns>True if the property is set, otherwise false.</returns>
public bool IsSet(PerspexProperty property)
{
Contract.Requires<NullReferenceException>(property != null);
@ -335,6 +335,33 @@ namespace Perspex
return this.values.ContainsKey(property);
}
/// <summary>
/// Checks whether a <see cref="PerspexProperty"/> is registered on this class.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>True if the property is registered, otherwise false.</returns>
private bool IsRegistered(PerspexProperty property)
{
Type type = this.GetType();
while (type != null)
{
List<PerspexProperty> list;
if (registered.TryGetValue(type, out list))
{
if (list.Contains(property))
{
return true;
}
}
type = type.GetTypeInfo().BaseType;
}
return false;
}
/// <summary>
/// Sets a <see cref="PerspexProperty"/> value.
/// </summary>
@ -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)

Loading…
Cancel
Save