diff --git a/src/Perspex.Base/PerspexObject.cs b/src/Perspex.Base/PerspexObject.cs index 6cb0c39bab..7c1317a7bc 100644 --- a/src/Perspex.Base/PerspexObject.cs +++ b/src/Perspex.Base/PerspexObject.cs @@ -501,6 +501,7 @@ namespace Perspex { Contract.Requires(property != null); VerifyAccess(); + if (property.IsDirect) { property = GetRegistered(property); @@ -511,7 +512,7 @@ namespace Perspex } LogPropertySet(property, value, priority); - property.Setter(this, value); + property.Setter(this, UnsetToDefault(value, property)); } else { @@ -848,6 +849,19 @@ namespace Perspex } } + /// + /// Converts an unset value to the default value for a property type. + /// + /// The value. + /// The property. + /// The value. + private static object UnsetToDefault(object value, PerspexProperty property) + { + return value == PerspexProperty.UnsetValue ? + TypeUtilities.Default(property.PropertyType) : + value; + } + /// /// Creates a for a . /// diff --git a/src/Perspex.Base/Utilities/TypeUtilities.cs b/src/Perspex.Base/Utilities/TypeUtilities.cs index f1eddf5b94..492aecd96c 100644 --- a/src/Perspex.Base/Utilities/TypeUtilities.cs +++ b/src/Perspex.Base/Utilities/TypeUtilities.cs @@ -127,5 +127,24 @@ namespace Perspex.Utilities return null; } } + + /// + /// Gets the default value for the specified type. + /// + /// The type. + /// The default value. + public static object Default(Type type) + { + var typeInfo = type.GetTypeInfo(); + + if (typeInfo.IsValueType) + { + return Activator.CreateInstance(type); + } + else + { + return null; + } + } } }