Browse Source

Update Owner when AddOwnering properties.

pull/297/head
Steven Kirk 11 years ago
parent
commit
6bd5f85d4d
  1. 56
      src/Perspex.Base/PerspexProperty.cs
  2. 28
      src/Perspex.Base/PerspexProperty`1.cs
  3. 18
      tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs

56
src/Perspex.Base/PerspexProperty.cs

@ -29,7 +29,12 @@ namespace Perspex
private static int s_nextId = 1;
/// <summary>
/// The default values for the property, by type.
/// The default value provided when the property was first registered.
/// </summary>
private readonly object _defaultValue;
/// <summary>
/// The overridden default values for the property, by type.
/// </summary>
private readonly Dictionary<Type, object> _defaultValues = new Dictionary<Type, object>();
@ -93,7 +98,7 @@ namespace Perspex
Name = name;
PropertyType = valueType;
OwnerType = ownerType;
_defaultValues.Add(ownerType, defaultValue);
_defaultValue = defaultValue;
Inherits = inherits;
DefaultBindingMode = defaultBindingMode;
IsAttached = isAttached;
@ -144,14 +149,57 @@ namespace Perspex
/// Initializes a new instance of the <see cref="PerspexProperty"/> class.
/// </summary>
/// <param name="source">The direct property to copy.</param>
/// <param name="ownerType">The new owner type.</param>
protected PerspexProperty(PerspexProperty source, Type ownerType)
{
Contract.Requires<ArgumentNullException>(source != null);
Contract.Requires<ArgumentNullException>(ownerType != null);
if (source.IsDirect)
{
throw new InvalidOperationException(
"This method cannot be called on direct PerspexProperties.");
}
//Name = name;
//PropertyType = valueType;
//OwnerType = ownerType;
//_defaultValues.Add(ownerType, defaultValue);
//Inherits = inherits;
//DefaultBindingMode = defaultBindingMode;
//IsAttached = isAttached;
//Notifying = notifying;
//_id = s_nextId++;
Name = source.Name;
PropertyType = source.PropertyType;
OwnerType = ownerType;
_defaultValue = source._defaultValue;
_defaultValues = source._defaultValues;
Inherits = source.Inherits;
DefaultBindingMode = source.DefaultBindingMode;
IsAttached = false;
Notifying = Notifying;
_validation = source._validation;
_id = source._id;
}
/// <summary>
/// Initializes a new instance of the <see cref="PerspexProperty"/> class.
/// </summary>
/// <param name="source">The direct property to copy.</param>
/// <param name="ownerType">The new owner type.</param>
/// <param name="getter">A new getter.</param>
/// <param name="setter">A new setter.</param>
protected PerspexProperty(
PerspexProperty source,
Type ownerType,
Func<PerspexObject, object> getter,
Action<PerspexObject, object> setter)
{
Contract.Requires<ArgumentNullException>(source != null);
Contract.Requires<ArgumentNullException>(ownerType != null);
Contract.Requires<ArgumentNullException>(getter != null);
if (!source.IsDirect)
@ -162,7 +210,7 @@ namespace Perspex
Name = source.Name;
PropertyType = source.PropertyType;
OwnerType = source.OwnerType;
OwnerType = ownerType;
Getter = getter;
Setter = setter;
IsDirect = true;
@ -530,7 +578,7 @@ namespace Perspex
type = type.GetTypeInfo().BaseType;
}
return _defaultValues[OwnerType];
return _defaultValue;
}
/// <summary>

28
src/Perspex.Base/PerspexProperty`1.cs

@ -66,17 +66,29 @@ namespace Perspex
Setter = setter;
}
/// <summary>
/// Initializes a new instance of the <see cref="PerspexProperty"/> class.
/// </summary>
/// <param name="source">The property to copy.</param>
/// <param name="ownerType">The new owner type.</param>
private PerspexProperty(PerspexProperty source, Type ownerType)
: base(source, ownerType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PerspexProperty"/> class.
/// </summary>
/// <param name="source">The direct property to copy.</param>
/// <param name="ownerType">The new owner type.</param>
/// <param name="getter">A new getter.</param>
/// <param name="setter">A new setter.</param>
private PerspexProperty(
PerspexProperty source,
Type ownerType,
Func<PerspexObject, TValue> getter,
Action<PerspexObject, TValue> setter)
: base(source, CastParamReturn(getter), CastParams(setter))
: base(source, ownerType, CastParamReturn(getter), CastParams(setter))
{
Getter = getter;
Setter = setter;
@ -105,8 +117,9 @@ namespace Perspex
"You must provide a new getter and setter when calling AddOwner on a direct PerspexProperty.");
}
PerspexPropertyRegistry.Instance.Register(typeof(TOwner), this);
return this;
var result = new PerspexProperty<TValue>(this, typeof(TOwner));
PerspexPropertyRegistry.Instance.Register(typeof(TOwner), result);
return result;
}
/// <summary>
@ -119,8 +132,15 @@ namespace Perspex
Action<TOwner, TValue> setter = null)
where TOwner : PerspexObject
{
if (!IsDirect)
{
throw new InvalidOperationException(
"This overload of AddOwner is for direct PerspexProperties.");
}
var result = new PerspexProperty<TValue>(
this,
this,
typeof(TOwner),
CastReturn(getter),
CastParam1(setter));

18
tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs

@ -161,6 +161,15 @@ namespace Perspex.Base.UnitTests
Assert.True(p1 == p2);
}
[Fact]
public void AddOwnered_Property_Should_Have_OwnerType_Set()
{
var p1 = new PerspexProperty<string>("p1", typeof(Class1));
var p2 = p1.AddOwner<Class3>();
Assert.Equal(typeof(Class3), p2.OwnerType);
}
[Fact]
public void AddOwnered_Direct_Property_Should_Equal_Original()
{
@ -172,6 +181,15 @@ namespace Perspex.Base.UnitTests
Assert.True(p1 == p2);
}
[Fact]
public void AddOwnered_Direct_Property_Should_Have_OwnerType_Set()
{
var p1 = new PerspexProperty<string>("d1", typeof(Class1), o => null, (o, v) => { });
var p2 = p1.AddOwner<Class3>(o => null, (o, v) => { });
Assert.Equal(typeof(Class3), p2.OwnerType);
}
[Fact]
public void AddOwner_With_Getter_And_Setter_On_Standard_Property_Should_Throw()
{

Loading…
Cancel
Save