diff --git a/Perspex.Base/Binding.cs b/Perspex.Base/Binding.cs deleted file mode 100644 index dc050b7e92..0000000000 --- a/Perspex.Base/Binding.cs +++ /dev/null @@ -1,76 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex -{ - using System; - using System.Reactive; - - public enum BindingMode - { - Default, - OneWay, - TwoWay, - OneTime, - OneWayToSource, - } - - public class Binding : ObservableBase, IDescription - { - public BindingMode Mode - { - get; - set; - } - - public BindingPriority Priority - { - get; - set; - } - - public PerspexProperty Property - { - get; - set; - } - - public PerspexObject Source - { - get; - set; - } - - public string Description => string.Format("{0}.{1}", this.Source?.GetType().Name, this.Property.Name); - - public static Binding operator !(Binding binding) - { - return binding.WithMode(BindingMode.TwoWay); - } - - public static Binding operator ~(Binding binding) - { - return binding.WithMode(BindingMode.TwoWay); - } - - public Binding WithMode(BindingMode mode) - { - this.Mode = mode; - return this; - } - - public Binding WithPriority(BindingPriority priority) - { - this.Priority = priority; - return this; - } - - protected override IDisposable SubscribeCore(IObserver observer) - { - return this.Source.GetObservable(this.Property).Subscribe(observer); - } - } -} diff --git a/Perspex.Base/BindingDescriptor.cs b/Perspex.Base/BindingDescriptor.cs new file mode 100644 index 0000000000..1694b6dfca --- /dev/null +++ b/Perspex.Base/BindingDescriptor.cs @@ -0,0 +1,137 @@ +// ----------------------------------------------------------------------- +// +// 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); + } + } +} diff --git a/Perspex.Base/Perspex.Base.csproj b/Perspex.Base/Perspex.Base.csproj index ed2f40d64d..82013168f7 100644 --- a/Perspex.Base/Perspex.Base.csproj +++ b/Perspex.Base/Perspex.Base.csproj @@ -37,7 +37,7 @@ 4 - + diff --git a/Perspex.Base/PerspexObject.cs b/Perspex.Base/PerspexObject.cs index b4cbf4f818..0c0e402c36 100644 --- a/Perspex.Base/PerspexObject.cs +++ b/Perspex.Base/PerspexObject.cs @@ -199,11 +199,11 @@ namespace Perspex /// Gets or sets a binding for a . /// /// The binding information. - public IObservable this[Binding binding] + public IObservable this[BindingDescriptor binding] { get { - return new Binding + return new BindingDescriptor { Mode = binding.Mode, Priority = binding.Priority, @@ -214,10 +214,10 @@ namespace Perspex set { - BindingMode mode = (binding.Mode == BindingMode.Default) ? + var mode = (binding.Mode == BindingMode.Default) ? binding.Property.DefaultBindingMode : binding.Mode; - Binding sourceBinding = value as Binding; + var sourceBinding = value as BindingDescriptor; if (sourceBinding == null && mode != BindingMode.OneWay) { diff --git a/Perspex.Base/PerspexProperty.cs b/Perspex.Base/PerspexProperty.cs index 4cf1e046ab..8264d87b1c 100644 --- a/Perspex.Base/PerspexProperty.cs +++ b/Perspex.Base/PerspexProperty.cs @@ -170,10 +170,10 @@ namespace Perspex /// indexer. /// /// The property. - /// A describing the binding. - public static Binding operator !(PerspexProperty property) + /// A describing the binding. + public static BindingDescriptor operator !(PerspexProperty property) { - return new Binding + return new BindingDescriptor { Priority = BindingPriority.LocalValue, Property = property, @@ -185,10 +185,10 @@ namespace Perspex /// indexer. /// /// The property. - /// A describing the binding. - public static Binding operator ~(PerspexProperty property) + /// A describing the binding. + public static BindingDescriptor operator ~(PerspexProperty property) { - return new Binding + return new BindingDescriptor { Priority = BindingPriority.TemplatedParent, Property = property, @@ -305,13 +305,13 @@ namespace Perspex /// Returns a binding accessor that can be passed to 's [] /// operator to initiate a binding. /// - /// A . + /// A . /// /// The ! and ~ operators are short forms of this. /// - public Binding Bind() + public BindingDescriptor Bind() { - return new Binding + return new BindingDescriptor { Property = this, }; diff --git a/Tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs b/Tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs index eea8f421b3..fe8e3eade7 100644 --- a/Tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs +++ b/Tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs @@ -226,7 +226,7 @@ namespace Perspex.Base.UnitTests { Class1 target1 = new Class1(); Class1 target2 = new Class1(); - Binding binding = Class1.FooProperty.Bind().WithMode(BindingMode.OneWay); + BindingDescriptor binding = Class1.FooProperty.Bind().WithMode(BindingMode.OneWay); target1.SetValue(Class1.FooProperty, "first"); target2[binding] = target1[!Class1.FooProperty]; @@ -240,7 +240,7 @@ namespace Perspex.Base.UnitTests { Class1 target1 = new Class1(); Class1 target2 = new Class1(); - Binding binding = Class1.FooProperty.Bind().WithMode(BindingMode.TwoWay); + BindingDescriptor binding = Class1.FooProperty.Bind().WithMode(BindingMode.TwoWay); target1.SetValue(Class1.FooProperty, "first"); target2[binding] = target1[!Class1.FooProperty]; @@ -256,7 +256,7 @@ namespace Perspex.Base.UnitTests { Class1 target1 = new Class1(); Class1 target2 = new Class1(); - Binding binding = Class1.FooProperty.Bind().WithMode(BindingMode.OneTime); + BindingDescriptor binding = Class1.FooProperty.Bind().WithMode(BindingMode.OneTime); target1.SetValue(Class1.FooProperty, "first"); target2[binding] = target1[!Class1.FooProperty];