Browse Source

Renamed Binding -> BindingDescriptor

And documented it.
pull/81/head
Steven Kirk 11 years ago
parent
commit
e56b189124
  1. 76
      Perspex.Base/Binding.cs
  2. 137
      Perspex.Base/BindingDescriptor.cs
  3. 2
      Perspex.Base/Perspex.Base.csproj
  4. 8
      Perspex.Base/PerspexObject.cs
  5. 18
      Perspex.Base/PerspexProperty.cs
  6. 6
      Tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs

76
Perspex.Base/Binding.cs

@ -1,76 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="Binding.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Reactive;
public enum BindingMode
{
Default,
OneWay,
TwoWay,
OneTime,
OneWayToSource,
}
public class Binding : ObservableBase<object>, 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<object> observer)
{
return this.Source.GetObservable(this.Property).Subscribe(observer);
}
}
}

137
Perspex.Base/BindingDescriptor.cs

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

2
Perspex.Base/Perspex.Base.csproj

@ -37,7 +37,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Binding.cs" />
<Compile Include="BindingDescriptor.cs" />
<Compile Include="Diagnostics\PerspexObjectExtensions.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="IObservablePropertyBag.cs" />

8
Perspex.Base/PerspexObject.cs

@ -199,11 +199,11 @@ namespace Perspex
/// Gets or sets a binding for a <see cref="PerspexProperty"/>.
/// </summary>
/// <param name="binding">The binding information.</param>
public IObservable<object> this[Binding binding]
public IObservable<object> 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)
{

18
Perspex.Base/PerspexProperty.cs

@ -170,10 +170,10 @@ namespace Perspex
/// indexer.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>A <see cref="Binding"/> describing the binding.</returns>
public static Binding operator !(PerspexProperty property)
/// <returns>A <see cref="BindingDescriptor"/> describing the binding.</returns>
public static BindingDescriptor operator !(PerspexProperty property)
{
return new Binding
return new BindingDescriptor
{
Priority = BindingPriority.LocalValue,
Property = property,
@ -185,10 +185,10 @@ namespace Perspex
/// indexer.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>A <see cref="Binding"/> describing the binding.</returns>
public static Binding operator ~(PerspexProperty property)
/// <returns>A <see cref="BindingDescriptor"/> describing the binding.</returns>
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 <see cref="PerspexObject"/>'s []
/// operator to initiate a binding.
/// </summary>
/// <returns>A <see cref="Binding"/>.</returns>
/// <returns>A <see cref="BindingDescriptor"/>.</returns>
/// <remarks>
/// The ! and ~ operators are short forms of this.
/// </remarks>
public Binding Bind()
public BindingDescriptor Bind()
{
return new Binding
return new BindingDescriptor
{
Property = this,
};

6
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];

Loading…
Cancel
Save