Browse Source

Allow binding to plain IObservables.

pull/4/head
Steven Kirk 12 years ago
parent
commit
ecbb40419c
  1. 10
      Perspex.Base/Binding.cs
  2. 16
      Perspex.Base/PerspexObject.cs
  3. 2
      packages/repositories.config

10
Perspex.Base/Binding.cs

@ -6,6 +6,9 @@
namespace Perspex
{
using System;
using System.Reactive.Linq;
public enum BindingMode
{
Default,
@ -15,7 +18,7 @@ namespace Perspex
OneWayToSource,
}
public struct Binding
public class Binding : IObservable<object>
{
public BindingMode Mode
{
@ -51,6 +54,11 @@ namespace Perspex
return binding.WithMode(BindingMode.TwoWay);
}
public IDisposable Subscribe(IObserver<object> observer)
{
return this.Source.GetObservable(Property).Subscribe(observer);
}
public Binding WithMode(BindingMode mode)
{
this.Mode = mode;

16
Perspex.Base/PerspexObject.cs

@ -143,7 +143,7 @@ namespace Perspex
/// Gets or sets a binding for a <see cref="PerspexProperty"/>.
/// </summary>
/// <param name="binding">The binding information.</param>
public Binding this[Binding binding]
public IObservable<object> this[Binding binding]
{
get
{
@ -161,21 +161,27 @@ namespace Perspex
BindingMode mode = (binding.Mode == BindingMode.Default) ?
binding.Property.DefaultBindingMode :
binding.Mode;
Binding sourceBinding = value as Binding;
if (sourceBinding == null && mode != BindingMode.OneWay)
{
throw new InvalidOperationException("Can only bind OneWay to plain IObservable.");
}
switch (mode)
{
case BindingMode.Default:
case BindingMode.OneWay:
this.Bind(binding.Property, value.Source.GetObservable(value.Property), binding.Priority);
this.Bind(binding.Property, value, binding.Priority);
break;
case BindingMode.OneTime:
this.SetValue(binding.Property, value.Source.GetValue(value.Property));
this.SetValue(binding.Property, sourceBinding.Source.GetValue(sourceBinding.Property));
break;
case BindingMode.OneWayToSource:
value.Source.Bind(value.Property, this.GetObservable(binding.Property), binding.Priority);
sourceBinding.Source.Bind(sourceBinding.Property, this.GetObservable(binding.Property), binding.Priority);
break;
case BindingMode.TwoWay:
this.BindTwoWay(binding.Property, value.Source, value.Property);
this.BindTwoWay(binding.Property, sourceBinding.Source, sourceBinding.Property);
break;
}
}

2
packages/repositories.config

@ -4,6 +4,7 @@
<repository path="..\Perspex.Base\packages.config" />
<repository path="..\Perspex.Controls.UnitTests\packages.config" />
<repository path="..\Perspex.Controls\packages.config" />
<repository path="..\Perspex.Diagnostics\packages.config" />
<repository path="..\Perspex.Direct2D1.RenderTests\packages.config" />
<repository path="..\Perspex.Direct2D1\packages.config" />
<repository path="..\Perspex.Input\packages.config" />
@ -16,6 +17,5 @@
<repository path="..\Perspex.Themes.Default\packages.config" />
<repository path="..\Perspex.UnitTests\packages.config" />
<repository path="..\Perspex.Windows\packages.config" />
<repository path="..\Perspex\packages.config" />
<repository path="..\TestApplication\packages.config" />
</repositories>
Loading…
Cancel
Save