// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
///
/// A typed perspex property.
///
/// The value type of the property.
public class PerspexProperty : PerspexProperty
{
///
/// Initializes a new instance of the class.
///
/// The name of the property.
/// The type of the class that registers the property.
/// The default value of the property.
/// Whether the property inherits its value.
/// The default binding mode for the property.
/// A validation function.
/// Whether the property is an attached property.
public PerspexProperty(
string name,
Type ownerType,
TValue defaultValue = default(TValue),
bool inherits = false,
BindingMode defaultBindingMode = BindingMode.Default,
Func validate = null,
bool isAttached = false)
: base(
name,
typeof(TValue),
ownerType,
defaultValue,
inherits,
defaultBindingMode,
Convert(validate),
isAttached)
{
Contract.Requires(name != null);
Contract.Requires(ownerType != null);
}
///
/// Registers the property on another type.
///
/// The type of the additional owner.
/// The property.
public PerspexProperty AddOwner()
{
PerspexObject.Register(typeof(TOwner), this);
return this;
}
///
/// Gets the default value for the property on the specified type.
///
/// The type.
/// The default value.
public TValue GetDefaultValue()
{
return (TValue)this.GetDefaultValue(typeof(T));
}
///
/// Overrides the validation function for the property on the specified type.
///
/// The type.
/// The validation function.
public void OverrideValidation(Func validation) where T : PerspexObject
{
var f = validation != null ?
(o, v) => validation((T)o, (TValue)v) :
(Func)null;
this.OverrideValidation(typeof(T), f);
}
///
/// Converts from a typed validation function to an untyped.
///
/// The typed validation function.
/// The untyped validation function.
private static Func Convert(Func f)
{
return f != null ? (o, v) => f(o, (TValue)v) : (Func)null;
}
}
}