From ecbb40419c875ee0603ebe3dad8e82ba32f46e37 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 23 Sep 2014 14:39:49 +0200 Subject: [PATCH] Allow binding to plain IObservables. --- Perspex.Base/Binding.cs | 10 +++++++++- Perspex.Base/PerspexObject.cs | 16 +++++++++++----- packages/repositories.config | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Perspex.Base/Binding.cs b/Perspex.Base/Binding.cs index 702b48ae1e..6ad04a90cd 100644 --- a/Perspex.Base/Binding.cs +++ b/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 { public BindingMode Mode { @@ -51,6 +54,11 @@ namespace Perspex return binding.WithMode(BindingMode.TwoWay); } + public IDisposable Subscribe(IObserver observer) + { + return this.Source.GetObservable(Property).Subscribe(observer); + } + public Binding WithMode(BindingMode mode) { this.Mode = mode; diff --git a/Perspex.Base/PerspexObject.cs b/Perspex.Base/PerspexObject.cs index 1a8fbce390..7c2db48fda 100644 --- a/Perspex.Base/PerspexObject.cs +++ b/Perspex.Base/PerspexObject.cs @@ -143,7 +143,7 @@ namespace Perspex /// Gets or sets a binding for a . /// /// The binding information. - public Binding this[Binding binding] + public IObservable 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; } } diff --git a/packages/repositories.config b/packages/repositories.config index 1ea0021a5f..eccb1c7d7e 100644 --- a/packages/repositories.config +++ b/packages/repositories.config @@ -4,6 +4,7 @@ + @@ -16,6 +17,5 @@ - \ No newline at end of file