2 using System.Collections;
3 using System.Runtime.CompilerServices;
4 using System.ComponentModel;
6 namespace Tizen.NUI.Binding
9 /// An abstract class that provides a BindingMode and a formatting option.
11 [EditorBrowsable(EditorBrowsableState.Never)]
12 public abstract class BindingBase
14 static readonly ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext> SynchronizedCollections = new ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext>();
16 BindingMode _mode = BindingMode.Default;
19 internal BindingBase()
24 /// Gets or sets the mode for this binding.
26 public BindingMode Mode
31 if ( value != BindingMode.Default
32 && value != BindingMode.OneWay
33 && value != BindingMode.OneWayToSource
34 && value != BindingMode.TwoWay
35 && value != BindingMode.OneTime)
36 throw new ArgumentException("mode is not a valid BindingMode", "mode");
45 /// Gets or sets the string format for this binding.
47 public string StringFormat
49 get { return _stringFormat; }
54 _stringFormat = value;
58 internal bool AllowChaining { get; set; }
60 internal object Context { get; set; }
62 internal bool IsApplied { get; private set; }
65 /// Stops synchronization on the collection.
67 /// <param name="collection">The collection on which to stop synchronization.</param>
68 public static void DisableCollectionSynchronization(IEnumerable collection)
70 if (collection == null)
71 throw new ArgumentNullException(nameof(collection));
73 SynchronizedCollections.Remove(collection);
76 internal static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
78 if (collection == null)
79 throw new ArgumentNullException(nameof(collection));
81 throw new ArgumentNullException(nameof(callback));
83 SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
87 /// Throws an InvalidOperationException if the binding has been applied.
89 protected void ThrowIfApplied()
92 throw new InvalidOperationException("Can not change a binding while it's applied");
95 internal virtual void Apply(bool fromTarget)
100 internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
105 internal abstract BindingBase Clone();
107 internal virtual object GetSourceValue(object value, Type targetPropertyType)
109 if (StringFormat != null)
110 return string.Format(StringFormat, value);
115 internal virtual object GetTargetValue(object value, Type sourcePropertyType)
120 internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
122 if (collection == null)
123 throw new ArgumentNullException(nameof(collection));
125 return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
128 internal virtual void Unapply(bool fromBindingContextChanged = false)