// -----------------------------------------------------------------------
//
// Copyright 2013 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using Splat;
///
/// The priority of a binding.
///
public enum BindingPriority
{
///
/// A local value.
///
LocalValue,
///
/// A triggered style binding.
///
///
/// A style trigger is a selector such as .class which overrides a
/// binding. In this way, a basic control can have
/// for example a Background from the templated parent which changes when the
/// control has the :pointerover class.
///
StyleTrigger,
///
/// A binding to a property on the templated parent.
///
TemplatedParent,
///
/// A style binding.
///
Style,
}
///
/// An object with support.
///
///
/// This class is analogous to DependencyObject in WPF.
///
public class PerspexObject : IEnableLogger
{
///
/// The registered properties by type.
///
private static Dictionary> registered =
new Dictionary>();
///
/// The parent object that inherited values are inherited from.
///
private PerspexObject inheritanceParent;
///
/// The set values/bindings on this object.
///
private Dictionary values =
new Dictionary();
///
/// Raised when a value changes on this object/
///
public event EventHandler PropertyChanged;
///
/// Gets or sets the parent object that inherited values
/// are inherited from.
///
protected PerspexObject InheritanceParent
{
get
{
return this.inheritanceParent;
}
set
{
if (this.inheritanceParent != value)
{
if (this.inheritanceParent != null)
{
this.inheritanceParent.PropertyChanged -= this.ParentPropertyChanged;
}
var inherited = (from property in GetProperties(this.GetType())
where property.Inherits
select new
{
Property = property,
Value = this.GetValue(property),
}).ToList();
this.inheritanceParent = value;
foreach (var i in inherited)
{
object newValue = this.GetValue(i.Property);
if (!object.Equals(i.Value, newValue))
{
this.RaisePropertyChanged(i.Property, i.Value, newValue);
}
}
if (this.inheritanceParent != null)
{
this.inheritanceParent.PropertyChanged += this.ParentPropertyChanged;
}
}
}
}
///
/// Gets or sets the value of a .
///
/// The property.
public object this[PerspexProperty property]
{
get { return this.GetValue(property); }
set { this.SetValue(property, value); }
}
///
/// Gets or sets the binding for a .
///
/// The property.
public IObservable