2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 using System.Reflection;
6 using System.Runtime.CompilerServices;
7 using Tizen.NUI.Binding.Internals;
9 namespace Tizen.NUI.Binding
12 /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another, by enabling validation, type coercion, and an event system.
14 [EditorBrowsable(EditorBrowsableState.Never)]
15 public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
18 /// Implements the bound property whose interface is provided by the BindingContext property.
20 [EditorBrowsable(EditorBrowsableState.Never)]
21 public static readonly BindableProperty BindingContextProperty =
22 BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object),
23 BindingMode.OneWay, null, BindingContextPropertyChanged, null, null, BindingContextPropertyBindingChanging);
25 readonly List<BindablePropertyContext> _properties = new List<BindablePropertyContext>(4);
28 object _inheritedContext;
31 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
33 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34 [EditorBrowsable(EditorBrowsableState.Never)]
35 public object BindingContext
37 get { return _inheritedContext ?? GetValue(BindingContextProperty); }
38 set { SetValue(BindingContextProperty, value); }
41 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
43 SetDynamicResource(property, key, false);
47 /// Raised when a property has changed.
49 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 public event PropertyChangedEventHandler PropertyChanged;
53 /// <summary>Whether to allow null value when <seealso cref="CopyFrom"/>.</summary>
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 protected static bool AllowNullCopy { get; set; } = false;
57 /// <summary>Copy properties of other ViewStyle to this.</summary>
58 /// <param name="other">The other BindableProperty merge to this.</param>
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public virtual void CopyFrom(BindableObject other)
62 if (null == other) return;
64 Type type1 = this.GetType();
65 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
67 Type type2 = other.GetType();
68 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
70 if (null != nameToBindableProperty1)
72 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
74 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
76 if (null != bindableProperty)
78 object value = other.GetValue(bindableProperty);
80 if (AllowNullCopy || null != value)
82 SetValue(keyValuePair.Value, value);
90 /// Raised whenever the BindingContext property changes.
92 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
93 [EditorBrowsable(EditorBrowsableState.Never)]
94 public event EventHandler BindingContextChanged;
96 internal void ClearValue(BindableProperty property, bool fromStyle)
98 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
102 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
104 /// <param name="property">The BindableProperty to clear</param>
105 internal void ClearValue(BindableProperty property)
107 ClearValue(property, fromStyle: false, checkAccess: true);
111 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
113 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
114 internal void ClearValue(BindablePropertyKey propertyKey)
116 if (propertyKey == null)
117 throw new ArgumentNullException(nameof(propertyKey));
119 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
123 /// Return true if the target property exists and has been set.
125 /// <param name="targetProperty">The target property</param>
126 /// <returns>return true if the target property exists and has been set</returns>
127 internal bool IsSet(BindableProperty targetProperty)
129 if (targetProperty == null)
130 throw new ArgumentNullException(nameof(targetProperty));
132 var bpcontext = GetContext(targetProperty);
133 return bpcontext != null
134 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
138 /// Returns the value that is contained the BindableProperty.
140 /// <param name="property">The BindableProperty for which to get the value.</param>
141 /// <returns>The value that is contained the BindableProperty</returns>
142 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 public object GetValue(BindableProperty property)
146 if (property == null)
147 throw new ArgumentNullException(nameof(property));
149 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
152 return property.DefaultValue;
154 return context.Value;
158 /// Raised when a property is about to change.
160 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public event PropertyChangingEventHandler PropertyChanging;
165 /// Removes a previously set binding.
167 /// <param name="property">The BindableProperty from which to remove bindings.</param>
168 internal void RemoveBinding(BindableProperty property)
170 if (property == null)
171 throw new ArgumentNullException(nameof(property));
173 BindablePropertyContext context = GetContext(property);
174 if (context == null || context.Binding == null)
177 RemoveBinding(property, context);
181 /// Assigns a binding to a property.
183 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
184 /// <param name="binding">The binding to set.</param>
185 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
186 [EditorBrowsable(EditorBrowsableState.Never)]
187 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
189 SetBinding(targetProperty, binding, false);
192 private bool isCreateByXaml = false;
193 /// Only used by the IL of xaml, will never changed to not hidden.
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 public virtual bool IsCreateByXaml
199 return isCreateByXaml;
203 isCreateByXaml = value;
208 /// Sets the value of the specified property.
210 /// <param name="property">The BindableProperty on which to assign a value.</param>
211 /// <param name="value">The value to set.</param>
212 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
213 [EditorBrowsable(EditorBrowsableState.Never)]
214 public void SetValue(BindableProperty property, object value)
216 if (true == isCreateByXaml)
218 SetValue(property, value, false, true);
222 property.PropertyChanged?.Invoke(this, null, value);
224 OnPropertyChanged(property.PropertyName);
228 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
230 if (property == null)
231 throw new ArgumentNullException(nameof(property));
233 if (true == isCreateByXaml)
235 if (property.IsReadOnly)
236 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
238 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
239 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
243 property.PropertyChanged?.Invoke(this, null, value);
248 /// Sets the value of the propertyKey.
250 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
251 /// <param name="value">The value to set.</param>
252 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
253 [EditorBrowsable(EditorBrowsableState.Never)]
254 public void SetValue(BindablePropertyKey propertyKey, object value)
256 if (propertyKey == null)
257 throw new ArgumentNullException(nameof(propertyKey));
259 SetValue(propertyKey.BindableProperty, value, false, false);
263 /// Set the inherited context to a neated element.
265 /// <param name="bindable">The object on which to set the inherited binding context.</param>
266 /// <param name="value">The inherited context to set.</param>
267 [EditorBrowsable(EditorBrowsableState.Never)]
268 public static void SetInheritedBindingContext(BindableObject bindable, object value)
270 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
271 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
274 object oldContext = bindable._inheritedContext;
276 if (ReferenceEquals(oldContext, value))
279 if (bpContext != null && oldContext == null)
280 oldContext = bpContext.Value;
282 if (bpContext != null && bpContext.Binding != null)
284 bpContext.Binding.Context = value;
285 bindable._inheritedContext = null;
289 bindable._inheritedContext = value;
292 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
293 bindable.OnBindingContextChanged();
297 /// Apply the bindings to BindingContext.
299 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
300 [EditorBrowsable(EditorBrowsableState.Never)]
301 protected void ApplyBindings()
303 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
307 /// Override this method to execute an action when the BindingContext changes.
309 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
310 [EditorBrowsable(EditorBrowsableState.Never)]
311 protected virtual void OnBindingContextChanged()
313 BindingContextChanged?.Invoke(this, EventArgs.Empty);
317 /// Call this method from a child class to notify that a change happened on a property.
319 /// <param name="propertyName">The name of the property that changed.</param>
320 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
321 [EditorBrowsable(EditorBrowsableState.Never)]
322 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
323 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
326 /// Call this method from a child class to notify that a change is going to happen on a property.
328 /// <param name="propertyName">The name of the property that is changing.</param>
329 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
330 [EditorBrowsable(EditorBrowsableState.Never)]
331 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
332 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
335 /// Unapplies all previously set bindings.
337 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
338 [EditorBrowsable(EditorBrowsableState.Never)]
339 protected void UnapplyBindings()
341 for (int i = 0, _propertiesCount = _properties.Count; i < _propertiesCount; i++)
343 BindablePropertyContext context = _properties[i];
344 if (context.Binding == null)
347 context.Binding.Unapply();
351 internal bool GetIsBound(BindableProperty targetProperty)
353 if (targetProperty == null)
354 throw new ArgumentNullException(nameof(targetProperty));
356 BindablePropertyContext bpcontext = GetContext(targetProperty);
357 return bpcontext != null && bpcontext.Binding != null;
361 /// Returns the value that is contained the BindableProperty.
363 /// <param name="property0">The BindableProperty instance.</param>
364 /// <param name="property1">The BindableProperty instance.</param>
365 /// <returns>The value that is contained the BindableProperty</returns>
366 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
368 var values = new object[2];
370 for (var i = 0; i < _properties.Count; i++)
372 BindablePropertyContext context = _properties[i];
374 if (ReferenceEquals(context.Property, property0))
376 values[0] = context.Value;
379 else if (ReferenceEquals(context.Property, property1))
381 values[1] = context.Value;
385 if (property0 == null && property1 == null)
389 if (!ReferenceEquals(property0, null))
390 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
391 if (!ReferenceEquals(property1, null))
392 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
398 /// Returns the value that is contained the BindableProperty.
400 /// <param name="property0">The BindableProperty instance.</param>
401 /// <param name="property1">The BindableProperty instance.</param>
402 /// <param name="property2">The BindableProperty instance.</param>
403 /// <returns>The value that is contained the BindableProperty</returns>
404 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
406 var values = new object[3];
408 for (var i = 0; i < _properties.Count; i++)
410 BindablePropertyContext context = _properties[i];
412 if (ReferenceEquals(context.Property, property0))
414 values[0] = context.Value;
417 else if (ReferenceEquals(context.Property, property1))
419 values[1] = context.Value;
422 else if (ReferenceEquals(context.Property, property2))
424 values[2] = context.Value;
428 if (property0 == null && property1 == null && property2 == null)
432 if (!ReferenceEquals(property0, null))
433 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
434 if (!ReferenceEquals(property1, null))
435 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
436 if (!ReferenceEquals(property2, null))
437 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
443 /// Returns the value that is contained the BindableProperty.
445 /// <param name="properties">The array of the BindableProperty instances</param>
446 /// <returns>The values that is contained the BindableProperty instances.</returns>
447 internal object[] GetValues(params BindableProperty[] properties)
449 var values = new object[properties.Length];
450 for (var i = 0; i < _properties.Count; i++)
452 var context = _properties[i];
453 var index = properties.IndexOf(context.Property);
456 values[index] = context.Value;
458 for (var i = 0; i < values.Length; i++)
460 if (!ReferenceEquals(values[i], null))
462 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
467 internal virtual void OnRemoveDynamicResource(BindableProperty property)
471 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
475 internal void RemoveDynamicResource(BindableProperty property)
477 if (property == null)
478 throw new ArgumentNullException(nameof(property));
480 OnRemoveDynamicResource(property);
481 BindablePropertyContext context = GetOrCreateContext(property);
482 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
485 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
487 if (targetProperty == null)
488 throw new ArgumentNullException(nameof(targetProperty));
490 throw new ArgumentNullException(nameof(binding));
492 if (fromStyle && !CanBeSetFromStyle(targetProperty))
495 var context = GetOrCreateContext(targetProperty);
497 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
499 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
501 if (context.Binding != null)
502 context.Binding.Unapply();
504 BindingBase oldBinding = context.Binding;
505 context.Binding = binding;
507 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
509 binding.Apply(BindingContext, this, targetProperty);
512 bool CanBeSetFromStyle(BindableProperty property)
514 var context = GetContext(property);
517 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
519 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
521 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
526 internal void SetDynamicResource(BindableProperty property, string key)
528 SetDynamicResource(property, key, false);
531 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
533 if (property == null)
534 throw new ArgumentNullException(nameof(property));
535 if (string.IsNullOrEmpty(key))
536 throw new ArgumentNullException(nameof(key));
537 if (fromStyle && !CanBeSetFromStyle(property))
540 var context = GetOrCreateContext(property);
542 context.Attributes |= BindableContextAttributes.IsDynamicResource;
544 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
546 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
548 OnSetDynamicResource(property, key);
551 internal void SetValue(BindableProperty property, object value, bool fromStyle)
553 SetValue(property, value, fromStyle, true);
556 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
558 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
562 /// For internal use.
564 /// <param name="property">The BindableProperty on which to assign a value.</param>
565 /// <param name="value">The value to set</param>
566 /// <param name="attributes">The set value flag</param>
567 [EditorBrowsable(EditorBrowsableState.Never)]
568 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
570 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
573 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
575 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
576 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
577 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
578 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
579 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
581 if (property == null)
582 throw new ArgumentNullException(nameof(property));
583 if (checkAccess && property.IsReadOnly)
585 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
589 if (!converted && !property.TryConvert(ref value))
591 Console.WriteLine("SetValue", "Can not convert {0} to type '{1}'", value, property.ReturnType);
595 if (property.ValidateValue != null && !property.ValidateValue(this, value))
596 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
598 if (property.CoerceValue != null)
599 value = property.CoerceValue(this, value);
601 BindablePropertyContext context = GetOrCreateContext(property);
604 context.Attributes |= BindableContextAttributes.IsManuallySet;
605 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
608 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
611 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
612 // else omitted on purpose
614 bool currentlyApplying = _applying;
616 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
618 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
619 if (delayQueue == null)
620 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
622 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
626 context.Attributes |= BindableContextAttributes.IsBeingSet;
627 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
629 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
630 if (delayQueue != null)
632 while (delayQueue.Count > 0)
634 SetValueArgs s = delayQueue.Dequeue();
635 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
638 context.DelayedSetters = null;
641 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
645 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
647 var prop = _properties.ToArray();
648 for (int i = 0, propLength = prop.Length; i < propLength; i++)
650 BindablePropertyContext context = prop[i];
651 BindingBase binding = context.Binding;
655 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
658 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
659 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
663 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
665 object context = bindable._inheritedContext;
666 var oldBinding = oldBindingBase as Binding;
667 var newBinding = newBindingBase as Binding;
669 if (context == null && oldBinding != null)
670 context = oldBinding.Context;
671 if (context != null && newBinding != null)
672 newBinding.Context = context;
675 static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
677 bindable._inheritedContext = null;
678 bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
679 bindable.OnBindingContextChanged();
682 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
684 if (property == null)
685 throw new ArgumentNullException(nameof(property));
687 if (checkAccess && property.IsReadOnly)
688 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
690 BindablePropertyContext bpcontext = GetContext(property);
691 if (bpcontext == null)
694 if (fromStyle && !CanBeSetFromStyle(property))
697 object original = bpcontext.Value;
699 object newValue = property.GetDefaultValue(this);
701 bool same = Equals(original, newValue);
704 property.PropertyChanging?.Invoke(this, original, newValue);
706 OnPropertyChanging(property.PropertyName);
709 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
710 bpcontext.Value = newValue;
711 if (property.DefaultValueCreator == null)
712 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
714 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
718 OnPropertyChanged(property.PropertyName);
719 property.PropertyChanged?.Invoke(this, original, newValue);
723 [MethodImpl(MethodImplOptions.AggressiveInlining)]
724 BindablePropertyContext CreateAndAddContext(BindableProperty property)
726 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
728 if (property.DefaultValueCreator == null)
729 context.Attributes = BindableContextAttributes.IsDefaultValue;
731 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
733 _properties.Add(context);
737 [MethodImpl(MethodImplOptions.AggressiveInlining)]
738 BindablePropertyContext GetContext(BindableProperty property)
740 List<BindablePropertyContext> properties = _properties;
742 for (var i = 0; i < properties.Count; i++)
744 BindablePropertyContext context = properties[i];
745 if (ReferenceEquals(context.Property, property))
752 [MethodImpl(MethodImplOptions.AggressiveInlining)]
753 BindablePropertyContext GetOrCreateContext(BindableProperty property)
755 BindablePropertyContext context = GetContext(property);
758 context = CreateAndAddContext(property);
760 else if (property.DefaultValueCreator != null)
762 context.Value = property.DefaultValueCreator(this); //Update Value from dali
768 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
770 context.Binding.Unapply();
772 property.BindingChanging?.Invoke(this, context.Binding, null);
774 context.Binding = null;
777 void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
779 if (property == null)
780 throw new ArgumentNullException(nameof(property));
782 if (checkAccess && property.IsReadOnly)
783 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
785 if (fromStyle && !CanBeSetFromStyle(property))
788 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
789 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
793 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
795 object original = context.Value;
796 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
797 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
798 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
799 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
801 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
802 if (!silent && (!same || raiseOnEqual))
804 property.PropertyChanging?.Invoke(this, original, value);
806 OnPropertyChanging(property.PropertyName);
809 if (!same || raiseOnEqual)
811 context.Value = value;
814 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
815 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
817 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
818 RemoveDynamicResource(property);
820 BindingBase binding = context.Binding;
823 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
825 RemoveBinding(property, context);
832 if ((!same || raiseOnEqual))
834 property.PropertyChanged?.Invoke(this, original, value);
836 if (binding != null && !currentlyApplying)
843 OnPropertyChanged(property.PropertyName);
845 else if (true == same && true == forceSendChangeSignal)
847 if (binding != null && !currentlyApplying)
854 OnPropertyChanged(property.PropertyName);
860 enum BindableContextAttributes
862 IsManuallySet = 1 << 0,
864 IsDynamicResource = 1 << 2,
865 IsSetFromStyle = 1 << 3,
866 IsDefaultValue = 1 << 4,
867 IsDefaultValueCreated = 1 << 5,
870 class BindablePropertyContext
872 public BindableContextAttributes Attributes;
873 public BindingBase Binding;
874 public Queue<SetValueArgs> DelayedSetters;
875 public BindableProperty Property;
880 internal enum SetValuePrivateFlags
883 CheckAccess = 1 << 0,
885 ManuallySet = 1 << 2,
888 Default = CheckAccess
893 public readonly SetValueFlags Attributes;
894 public readonly BindablePropertyContext Context;
895 public readonly bool CurrentlyApplying;
896 public readonly BindableProperty Property;
897 public readonly object Value;
899 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
904 CurrentlyApplying = currentlyApplying;
905 Attributes = attributes;
911 namespace Tizen.NUI.Binding.Internals
914 /// SetValueFlags. For internal use.
917 [EditorBrowsable(EditorBrowsableState.Never)]
918 public enum SetValueFlags
923 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
924 [EditorBrowsable(EditorBrowsableState.Never)]
928 /// Clear OneWay bindings.
930 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
931 [EditorBrowsable(EditorBrowsableState.Never)]
932 ClearOneWayBindings = 1 << 0,
935 /// Clear TwoWay bindings.
937 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
938 [EditorBrowsable(EditorBrowsableState.Never)]
939 ClearTwoWayBindings = 1 << 1,
942 /// Clear dynamic resource.
944 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
945 [EditorBrowsable(EditorBrowsableState.Never)]
946 ClearDynamicResource = 1 << 2,
951 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
952 [EditorBrowsable(EditorBrowsableState.Never)]
953 RaiseOnEqual = 1 << 3