// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex { using System; using System.Reactive; /// /// Defines possible binding modes. /// public enum BindingMode { /// /// Uses the default binding mode specified for the property. /// Default, /// /// Binds one way from source to target. /// OneWay, /// /// Binds two-way with the initial value coming from the target. /// TwoWay, /// /// Copies the target to the source one time and then disposes of the binding. /// OneTime, /// /// Binds one way from target to source. /// OneWayToSource, } /// /// Holds a description of a binding, usually for 's [] operator. /// public class BindingDescriptor : ObservableBase, IDescription { /// /// Gets or sets the binding mode. /// public BindingMode Mode { get; set; } /// /// Gets or sets the binding priority. /// public BindingPriority Priority { get; set; } /// /// Gets or sets the source property. /// public PerspexProperty Property { get; set; } /// /// Gets or sets the source object. /// public PerspexObject Source { get; set; } /// /// Gets a description of the binding. /// public string Description => string.Format("{0}.{1}", this.Source?.GetType().Name, this.Property.Name); /// /// Makes a two-way binding. /// /// The current binding. /// A two-way binding. public static BindingDescriptor operator !(BindingDescriptor binding) { return binding.WithMode(BindingMode.TwoWay); } /// /// Makes a two-way binding. /// /// The current binding. /// A two-way binding. public static BindingDescriptor operator ~(BindingDescriptor binding) { return binding.WithMode(BindingMode.TwoWay); } /// /// Modifies the binding mode. /// /// The binding mode. /// The object that the method was called on. public BindingDescriptor WithMode(BindingMode mode) { this.Mode = mode; return this; } /// /// Modifies the binding priority. /// /// The binding priority. /// The object that the method was called on. public BindingDescriptor WithPriority(BindingPriority priority) { this.Priority = priority; return this; } /// protected override IDisposable SubscribeCore(IObserver observer) { return this.Source.GetObservable(this.Property).Subscribe(observer); } } }