Browse Source

Added PerspexProperty.UnsetValue.

pull/4/head
grokys 12 years ago
parent
commit
8118ddf312
  1. 22
      Perspex/PerspexObject.cs
  2. 13
      Perspex/PerspexProperty.cs

22
Perspex/PerspexObject.cs

@ -184,10 +184,9 @@ namespace Perspex
/// <param name="property">The property.</param>
public void ClearValue(PerspexProperty property)
{
// TODO: Implement this by using SetValue(UnsetValue).
Contract.Requires<NullReferenceException>(property != null);
this.ClearBinding(property);
this.values.Remove(property);
this.SetValue(property, PerspexProperty.UnsetValue);
}
/// <summary>
@ -520,7 +519,7 @@ namespace Perspex
{
Contract.Requires<NullReferenceException>(property != null);
if (!property.IsValidType(value))
if (!property.IsValidValue(value))
{
throw new InvalidOperationException("Invalid value for " + property.Name);
}
@ -529,7 +528,18 @@ namespace Perspex
if (!object.Equals(oldValue, value))
{
this.values[property] = value;
string valueString = value.ToString();
if (value == PerspexProperty.UnsetValue)
{
valueString = "[Unset]";
this.values.Remove(property);
}
else
{
this.values[property] = value;
}
this.RaisePropertyChanged(property, oldValue, value);
this.Log().Debug(string.Format(
@ -537,7 +547,7 @@ namespace Perspex
this.GetType().Name,
property.Name,
this.GetHashCode(),
value));
valueString));
}
}

13
Perspex/PerspexProperty.cs

@ -22,6 +22,11 @@ namespace Perspex
/// </remarks>
public class PerspexProperty
{
/// <summary>
/// Represents an unset property value.
/// </summary>
public static readonly object UnsetValue = new object();
/// <summary>
/// The default values for the property, by type.
/// </summary>
@ -125,9 +130,13 @@ namespace Perspex
return this.defaultValues[this.OwnerType];
}
public bool IsValidType(object value)
public bool IsValidValue(object value)
{
if (value == null)
if (value == UnsetValue)
{
return true;
}
else if (value == null)
{
return !this.PropertyType.GetTypeInfo().IsValueType ||
Nullable.GetUnderlyingType(this.PropertyType) != null;

Loading…
Cancel
Save