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 internal abstract class BindingBase
14 static readonly ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext> SynchronizedCollections = new ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext>();
16 BindingMode _mode = BindingMode.Default;
18 object _targetNullValue;
19 object _fallbackValue;
21 internal BindingBase()
26 /// Gets or sets the mode for this binding.
28 public BindingMode Mode
33 if ( value != BindingMode.Default
34 && value != BindingMode.OneWay
35 && value != BindingMode.OneWayToSource
36 && value != BindingMode.TwoWay
37 && value != BindingMode.OneTime)
38 throw new ArgumentException("mode is not a valid BindingMode", "mode");
47 /// Gets or sets the string format for this binding.
49 public string StringFormat
51 get { return _stringFormat; }
56 _stringFormat = value;
60 public object TargetNullValue
62 get { return _targetNullValue; }
65 _targetNullValue = value;
69 public object FallbackValue {
70 get => _fallbackValue;
73 _fallbackValue = value;
77 internal bool AllowChaining { get; set; }
79 internal object Context { get; set; }
81 internal bool IsApplied { get; private set; }
84 /// Stops synchronization on the collection.
86 /// <param name="collection">The collection on which to stop synchronization.</param>
87 public static void DisableCollectionSynchronization(IEnumerable collection)
89 if (collection == null)
90 throw new ArgumentNullException(nameof(collection));
92 SynchronizedCollections.Remove(collection);
95 public static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
97 if (collection == null)
98 throw new ArgumentNullException(nameof(collection));
100 throw new ArgumentNullException(nameof(callback));
102 SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
106 /// Throws an InvalidOperationException if the binding has been applied.
108 protected void ThrowIfApplied()
111 throw new InvalidOperationException("Can not change a binding while it's applied");
114 internal virtual void Apply(bool fromTarget)
119 internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
124 internal abstract BindingBase Clone();
126 internal virtual object GetSourceValue(object value, Type targetPropertyType)
128 if (value == null && TargetNullValue != null)
129 return TargetNullValue;
130 if (StringFormat != null)
131 return string.Format(StringFormat, value);
136 internal virtual object GetTargetValue(object value, Type sourcePropertyType)
141 internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
143 if (collection == null)
144 throw new ArgumentNullException(nameof(collection));
146 return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
149 internal virtual void Unapply(bool fromBindingContextChanged = false)