2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Reflection;
24 using System.Runtime.CompilerServices;
25 using Tizen.NUI.Binding.Internals;
27 namespace Tizen.NUI.Binding
30 /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
32 /// <since_tizen> 9 </since_tizen>
33 public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
36 /// Implements the bound property whose interface is provided by the BindingContext property.
38 [EditorBrowsable(EditorBrowsableState.Never)]
39 public static readonly BindableProperty BindingContextProperty =
40 BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object), BindingMode.OneWay, null, BindingContextPropertyChanged,
41 null, null, BindingContextPropertyBindingChanging);
43 readonly Dictionary<BindableProperty, BindablePropertyContext> properties = new Dictionary<BindableProperty, BindablePropertyContext>(4);
46 object inheritedContext;
48 private object bindingContext;
51 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
53 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public object BindingContext
57 get { return GetValue(BindingContextProperty) ?? inheritedContext; }
58 set { SetValue(BindingContextProperty, value); }
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public int LineNumber { get; set; } = -1;
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 public int LinePosition { get; set; } = -1;
67 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
69 SetDynamicResource(property, key, false);
73 /// Raised when a property has changed.
75 /// <since_tizen> 9 </since_tizen>
76 public event PropertyChangedEventHandler PropertyChanged;
78 /// <summary>Copy properties of other ViewStyle to this.</summary>
79 /// <param name="other">The other BindableProperty merge to this.</param>
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public virtual void CopyFrom(BindableObject other)
83 if (null == other) return;
85 Type type1 = this.GetType();
86 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
88 Type type2 = other.GetType();
89 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
91 if (null != nameToBindableProperty1)
93 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
95 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
97 if (null != bindableProperty && (ChangedPropertiesSet.Contains(bindableProperty) || other.ChangedPropertiesSet.Contains(bindableProperty)))
99 object value = other.GetValue(bindableProperty);
103 InternalSetValue(keyValuePair.Value, value);
111 /// Copy all binding from other object.
113 /// <param name="other"></param>
114 [EditorBrowsable(EditorBrowsableState.Never)]
115 public void CopyBindingRelationShip(BindableObject other)
122 foreach (var property in properties)
124 RemoveBinding(property.Key);
127 foreach (var property in other.properties)
129 if (null != property.Value.Binding)
131 var binding = property.Value.Binding;
132 other.RemoveBinding(property.Key);
133 SetBinding(property.Key, binding);
139 /// Raised whenever the BindingContext property changes.
141 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public event EventHandler BindingContextChanged;
145 internal void ClearValue(BindableProperty property, bool fromStyle)
147 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
151 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
153 /// <param name="property">The BindableProperty to clear</param>
154 internal void ClearValue(BindableProperty property)
156 ClearValue(property, fromStyle: false, checkAccess: true);
160 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
162 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
163 internal void ClearValue(BindablePropertyKey propertyKey)
165 if (propertyKey == null)
166 throw new ArgumentNullException(nameof(propertyKey));
168 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
172 /// Return true if the target property exists and has been set.
174 /// <param name="targetProperty">The target property</param>
175 /// <returns>return true if the target property exists and has been set</returns>
176 internal bool IsSet(BindableProperty targetProperty)
178 if (targetProperty == null)
179 throw new ArgumentNullException(nameof(targetProperty));
181 var bpcontext = GetContext(targetProperty);
182 return bpcontext != null
183 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
187 /// Returns the value that is contained the BindableProperty.
189 /// <param name="property">The BindableProperty for which to get the value.</param>
190 /// <returns>The value that is contained the BindableProperty</returns>
191 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
192 [EditorBrowsable(EditorBrowsableState.Never)]
193 public object GetValue(BindableProperty property)
195 if (property == null)
196 throw new ArgumentNullException(nameof(property));
198 if (!IsBinded && property.ValueGetter != null)
200 return property.ValueGetter(this);
203 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
206 return property.DefaultValue;
208 return context.Value;
212 /// Raised when a property is about to change.
214 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 public event PropertyChangingEventHandler PropertyChanging;
219 /// Removes a previously set binding.
221 /// <param name="property">The BindableProperty from which to remove bindings.</param>
222 internal void RemoveBinding(BindableProperty property)
224 if (property == null)
225 throw new ArgumentNullException(nameof(property));
227 BindablePropertyContext context = GetContext(property);
228 if (context == null || context.Binding == null)
231 RemoveBinding(property, context);
235 /// Assigns a binding to a property.
237 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
238 /// <param name="binding">The binding to set.</param>
239 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
243 SetBinding(targetProperty, binding, false);
246 /// Internal used, will never changed to not hidden.
247 [EditorBrowsable(EditorBrowsableState.Never)]
248 public virtual bool IsCreateByXaml
254 /// This will be public opened in next ACR.
255 [EditorBrowsable(EditorBrowsableState.Never)]
256 public void EnforceNotifyBindedInstance(BindableProperty property)
258 if (null != property)
260 BindablePropertyContext context = GetOrCreateContext(property);
261 BindingBase binding = context.Binding;
263 var currentlyApplying = applying;
265 if (binding != null && !currentlyApplying)
272 OnPropertyChanged(property.PropertyName);
274 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
276 if (null != propertyGroup)
278 foreach (var relativeProperty in propertyGroup)
280 if (relativeProperty != property)
282 var relativeContext = GetOrCreateContext(relativeProperty);
283 var relativeBinding = relativeContext.Binding;
285 if (null != relativeBinding)
288 relativeBinding.Apply(true);
292 OnPropertyChanged(relativeProperty.PropertyName);
300 /// Sets the value of the specified property.
302 /// <param name="property">The BindableProperty on which to assign a value.</param>
303 /// <param name="value">The value to set.</param>
304 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
305 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
306 [EditorBrowsable(EditorBrowsableState.Never)]
307 public void SetValue(BindableProperty property, object value)
309 InternalSetValue(property, value);
310 ChangedPropertiesSetExcludingStyle.Add(property);
313 internal void InternalSetValue(BindableProperty property, object value)
315 if (true == IsBinded)
317 SetValue(property, value, false, true);
321 if (null == property)
323 throw new ArgumentNullException(nameof(property));
326 object oldvalue = null;
327 if (null == property.DefaultValueCreator)
329 BindablePropertyContext context = GetOrCreateContext(property);
332 oldvalue = context.Value;
333 context.Value = value;
338 oldvalue = property.DefaultValueCreator.Invoke(this);
341 property.PropertyChanged?.Invoke(this, oldvalue, value);
343 OnPropertyChanged(property.PropertyName);
344 OnPropertyChangedWithData(property);
347 ChangedPropertiesSet.Add(property);
350 private HashSet<BindableProperty> changedPropertiesSet;
351 private HashSet<BindableProperty> ChangedPropertiesSet
355 if (null == changedPropertiesSet)
357 changedPropertiesSet = new HashSet<BindableProperty>();
360 return changedPropertiesSet;
364 private HashSet<BindableProperty> changedPropertiesSetExcludingStyle;
365 internal protected HashSet<BindableProperty> ChangedPropertiesSetExcludingStyle
369 if (null == changedPropertiesSetExcludingStyle)
371 changedPropertiesSetExcludingStyle = new HashSet<BindableProperty>();
374 return changedPropertiesSetExcludingStyle;
379 /// Sets the value of the propertyKey.
381 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
382 /// <param name="value">The value to set.</param>
383 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
384 [EditorBrowsable(EditorBrowsableState.Never)]
385 public void SetValue(BindablePropertyKey propertyKey, object value)
387 if (propertyKey == null)
388 throw new ArgumentNullException(nameof(propertyKey));
390 SetValue(propertyKey.BindableProperty, value, false, false);
394 /// Set the inherited context to a neated element.
396 /// <param name="bindable">The object on which to set the inherited binding context.</param>
397 /// <param name="value">The inherited context to set.</param>
398 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
399 [EditorBrowsable(EditorBrowsableState.Never)]
400 public static void SetInheritedBindingContext(BindableObject bindable, object value)
402 if (null == bindable)
404 throw new ArgumentNullException(nameof(bindable));
407 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
408 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
411 object oldContext = bindable.inheritedContext;
413 if (ReferenceEquals(oldContext, value))
416 if (bpContext != null && oldContext == null)
417 oldContext = bpContext.Value;
419 if (bpContext != null && bpContext.Binding != null)
421 bpContext.Binding.Context = value;
422 bindable.inheritedContext = null;
426 bindable.inheritedContext = value;
429 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
430 bindable.OnBindingContextChanged();
434 /// Register the properties which can effect each other in Binding to same group.
436 [EditorBrowsable(EditorBrowsableState.Never)]
437 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
439 if (!PropertyToGroup.ContainsKey(property))
441 PropertyToGroup.Add(property, group);
444 if (null != group && !(group.Contains(property)))
450 /// Apply the bindings to BindingContext.
452 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
453 [EditorBrowsable(EditorBrowsableState.Never)]
454 protected void ApplyBindings()
456 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
460 /// Override this method to execute an action when the BindingContext changes.
462 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
463 [EditorBrowsable(EditorBrowsableState.Never)]
464 protected virtual void OnBindingContextChanged()
466 BindingContextChanged?.Invoke(this, EventArgs.Empty);
470 /// Call this method from a child class to notify that a change happened on a property.
472 /// <param name="propertyName">The name of the property that changed.</param>
473 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
474 [EditorBrowsable(EditorBrowsableState.Never)]
475 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
476 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
479 /// Call this method from a child class to notify that a change is going to happen on a property.
481 /// <param name="propertyName">The name of the property that is changing.</param>
482 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
483 [EditorBrowsable(EditorBrowsableState.Never)]
484 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
485 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
488 /// Method that is called when a bound property is changed.
490 [EditorBrowsable(EditorBrowsableState.Never)]
491 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
494 /// Unapplies all previously set bindings.
496 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
497 [EditorBrowsable(EditorBrowsableState.Never)]
498 protected void UnapplyBindings()
500 foreach (var context in properties.Values)
502 if (context.Binding == null)
505 context.Binding.Unapply();
509 internal bool GetIsBound(BindableProperty targetProperty)
511 if (targetProperty == null)
512 throw new ArgumentNullException(nameof(targetProperty));
514 BindablePropertyContext bpcontext = GetContext(targetProperty);
515 return bpcontext != null && bpcontext.Binding != null;
519 /// Returns the value that is contained the BindableProperty.
521 /// <param name="property0">The BindableProperty instance.</param>
522 /// <param name="property1">The BindableProperty instance.</param>
523 /// <returns>The value that is contained the BindableProperty</returns>
524 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
526 var values = new object[2];
528 foreach (var context in properties.Values)
530 if (ReferenceEquals(context.Property, property0))
532 values[0] = context.Value;
535 else if (ReferenceEquals(context.Property, property1))
537 values[1] = context.Value;
541 if (property0 == null && property1 == null)
545 if (!ReferenceEquals(property0, null))
546 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
547 if (!ReferenceEquals(property1, null))
548 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
554 /// Returns the value that is contained the BindableProperty.
556 /// <param name="property0">The BindableProperty instance.</param>
557 /// <param name="property1">The BindableProperty instance.</param>
558 /// <param name="property2">The BindableProperty instance.</param>
559 /// <returns>The value that is contained the BindableProperty</returns>
560 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
562 var values = new object[3];
564 foreach (var context in properties.Values)
566 if (ReferenceEquals(context.Property, property0))
568 values[0] = context.Value;
571 else if (ReferenceEquals(context.Property, property1))
573 values[1] = context.Value;
576 else if (ReferenceEquals(context.Property, property2))
578 values[2] = context.Value;
582 if (property0 == null && property1 == null && property2 == null)
586 if (!ReferenceEquals(property0, null))
587 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
588 if (!ReferenceEquals(property1, null))
589 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
590 if (!ReferenceEquals(property2, null))
591 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
597 /// Returns the value that is contained the BindableProperty.
599 /// <param name="properties">The array of the BindableProperty instances</param>
600 /// <returns>The values that is contained the BindableProperty instances.</returns>
601 internal object[] GetValues(params BindableProperty[] properties)
603 var values = new object[properties.Length];
604 foreach (var context in this.properties.Values)
606 var index = properties.IndexOf(context.Property);
609 values[index] = context.Value;
611 for (var i = 0; i < values.Length; i++)
613 if (!ReferenceEquals(values[i], null))
615 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
620 internal virtual void OnRemoveDynamicResource(BindableProperty property)
624 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
628 internal void RemoveDynamicResource(BindableProperty property)
630 if (property == null)
631 throw new ArgumentNullException(nameof(property));
633 OnRemoveDynamicResource(property);
634 BindablePropertyContext context = GetOrCreateContext(property);
635 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
638 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
640 if (targetProperty == null)
641 throw new ArgumentNullException(nameof(targetProperty));
643 throw new ArgumentNullException(nameof(binding));
645 if (fromStyle && !CanBeSetFromStyle(targetProperty))
650 var context = GetOrCreateContext(targetProperty);
652 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
654 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
656 if (context.Binding != null)
657 context.Binding.Unapply();
659 BindingBase oldBinding = context.Binding;
660 context.Binding = binding;
662 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
664 binding.Apply(BindingContext, this, targetProperty);
667 bool CanBeSetFromStyle(BindableProperty property)
669 var context = GetContext(property);
672 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
674 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
676 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
681 internal void SetDynamicResource(BindableProperty property, string key)
683 SetDynamicResource(property, key, false);
686 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
688 if (property == null)
689 throw new ArgumentNullException(nameof(property));
690 if (string.IsNullOrEmpty(key))
691 throw new ArgumentNullException(nameof(key));
692 if (fromStyle && !CanBeSetFromStyle(property))
695 var context = GetOrCreateContext(property);
697 context.Attributes |= BindableContextAttributes.IsDynamicResource;
699 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
701 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
703 OnSetDynamicResource(property, key);
706 internal void SetValue(BindableProperty property, object value, bool fromStyle)
708 SetValue(property, value, fromStyle, true);
711 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
713 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None);
717 /// For internal use.
719 /// <param name="property">The BindableProperty on which to assign a value.</param>
720 /// <param name="value">The value to set</param>
721 /// <param name="attributes">The set value flag</param>
722 [EditorBrowsable(EditorBrowsableState.Never)]
723 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
725 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default);
728 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
730 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
731 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
732 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
733 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
734 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
736 if (property == null)
737 throw new ArgumentNullException(nameof(property));
738 if (checkAccess && property.IsReadOnly)
740 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
744 if (!converted && !property.TryConvert(ref value))
746 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
750 if (property.ValidateValue != null && !property.ValidateValue(this, value))
751 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
753 if (property.CoerceValue != null)
754 value = property.CoerceValue(this, value);
756 BindablePropertyContext context = GetOrCreateContext(property);
759 context.Attributes |= BindableContextAttributes.IsManuallySet;
760 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
764 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
769 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
771 // else omitted on purpose
773 bool currentlyApplying = applying;
775 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
777 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
778 if (delayQueue == null)
779 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
781 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
785 context.Attributes |= BindableContextAttributes.IsBeingSet;
786 SetValueActual(property, context, value, currentlyApplying, attributes, silent);
788 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
789 if (delayQueue != null)
791 while (delayQueue.Count > 0)
793 SetValueArgs s = delayQueue.Dequeue();
794 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, s.Attributes);
797 context.DelayedSetters = null;
800 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
804 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
806 var prop = properties.Values.ToArray();
807 for (int i = 0, propLength = prop.Length; i < propLength; i++)
809 BindablePropertyContext context = prop[i];
810 BindingBase binding = context.Binding;
814 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
817 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
818 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
822 internal bool IsBinded
828 static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
830 bindable.inheritedContext = null;
831 bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
832 bindable.OnBindingContextChanged();
834 if (newvalue is BindableObject targetBindableObject)
836 targetBindableObject.IsBinded = true;
840 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
842 object context = bindable.inheritedContext;
843 var oldBinding = oldBindingBase as Binding;
844 var newBinding = newBindingBase as Binding;
846 if (context == null && oldBinding != null)
847 context = oldBinding.Context;
848 if (context != null && newBinding != null)
849 newBinding.Context = context;
852 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
854 if (property == null)
855 throw new ArgumentNullException(nameof(property));
857 if (checkAccess && property.IsReadOnly)
858 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
860 BindablePropertyContext bpcontext = GetContext(property);
861 if (bpcontext == null)
864 if (fromStyle && !CanBeSetFromStyle(property))
867 object original = bpcontext.Value;
869 object newValue = property.GetDefaultValue(this);
871 bool same = Equals(original, newValue);
874 property.PropertyChanging?.Invoke(this, original, newValue);
876 OnPropertyChanging(property.PropertyName);
879 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
880 bpcontext.Value = newValue;
881 if (property.DefaultValueCreator == null)
882 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
884 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
888 OnPropertyChanged(property.PropertyName);
889 OnPropertyChangedWithData(property);
890 property.PropertyChanged?.Invoke(this, original, newValue);
894 [MethodImpl(MethodImplOptions.AggressiveInlining)]
895 BindablePropertyContext CreateAndAddContext(BindableProperty property)
897 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
899 if (property.DefaultValueCreator == null)
900 context.Attributes = BindableContextAttributes.IsDefaultValue;
902 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
904 properties.Add(property, context);
908 [MethodImpl(MethodImplOptions.AggressiveInlining)]
909 BindablePropertyContext GetContext(BindableProperty property) => properties.TryGetValue(property, out var result) ? result : null;
911 [MethodImpl(MethodImplOptions.AggressiveInlining)]
912 BindablePropertyContext GetOrCreateContext(BindableProperty property)
914 BindablePropertyContext context = GetContext(property);
917 context = CreateAndAddContext(property);
919 else if (property.ValueGetter != null)
921 context.Value = property.ValueGetter(this); //Update Value from dali
922 }//added by xiaohui.fang
923 else if (property.DefaultValueCreator != null) //This will be removed in the future.
925 context.Value = property.DefaultValueCreator(this); //Update Value from dali
931 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
933 context.Binding.Unapply();
935 property.BindingChanging?.Invoke(this, context.Binding, null);
937 context.Binding = null;
940 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
942 if (property == null)
943 throw new ArgumentNullException(nameof(property));
945 if (checkAccess && property.IsReadOnly)
946 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
948 if (fromStyle && !CanBeSetFromStyle(property))
951 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
952 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0));
955 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, bool silent = false)
957 object original = context.Value;
958 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
959 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
960 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
961 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
963 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
964 if (!silent && (!same || raiseOnEqual))
966 property.PropertyChanging?.Invoke(this, original, value);
968 OnPropertyChanging(property.PropertyName);
971 if (!same || raiseOnEqual)
973 context.Value = value;
976 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
977 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
979 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
980 RemoveDynamicResource(property);
982 BindingBase binding = context.Binding;
985 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
987 RemoveBinding(property, context);
992 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
994 if (!silent && (!same || raiseOnEqual))
996 property.PropertyChanged?.Invoke(this, original, value);
998 if (binding != null && !currentlyApplying)
1001 binding.Apply(true);
1005 OnPropertyChanged(property.PropertyName);
1007 if (null != propertyGroup)
1009 foreach (var relativeProperty in propertyGroup)
1011 if (relativeProperty != property)
1013 var relativeContext = GetOrCreateContext(relativeProperty);
1014 var relativeBinding = relativeContext.Binding;
1016 if (null != relativeBinding)
1019 relativeBinding.Apply(true);
1023 OnPropertyChanged(relativeProperty.PropertyName);
1028 OnPropertyChangedWithData(property);
1032 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
1033 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1036 enum BindableContextAttributes
1038 IsManuallySet = 1 << 0,
1039 IsBeingSet = 1 << 1,
1040 IsDynamicResource = 1 << 2,
1041 IsSetFromStyle = 1 << 3,
1042 IsDefaultValue = 1 << 4,
1043 IsDefaultValueCreated = 1 << 5,
1046 class BindablePropertyContext
1048 public BindableContextAttributes Attributes;
1049 public BindingBase Binding;
1050 public Queue<SetValueArgs> DelayedSetters;
1051 public BindableProperty Property;
1052 public object Value;
1056 internal enum SetValuePrivateFlags
1059 CheckAccess = 1 << 0,
1061 ManuallySet = 1 << 2,
1064 Default = CheckAccess
1069 public readonly SetValueFlags Attributes;
1070 public readonly BindablePropertyContext Context;
1071 public readonly bool CurrentlyApplying;
1072 public readonly BindableProperty Property;
1073 public readonly object Value;
1075 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1077 Property = property;
1080 CurrentlyApplying = currentlyApplying;
1081 Attributes = attributes;
1085 internal void ReplaceBindingElement(Dictionary<string, object> oldNameScope, Dictionary<string, object> newNameScope)
1087 var xElementToNameOfOld = new Dictionary<object, string>();
1089 foreach (var pair in oldNameScope)
1091 xElementToNameOfOld.Add(pair.Value, pair.Key);
1094 foreach (var property in properties)
1096 if (property.Value.Binding is Binding binding && null != binding.Source)
1099 xElementToNameOfOld.TryGetValue(binding.Source, out xName);
1103 var newObject = newNameScope[xName];
1105 binding.Source = newObject;
1106 SetBinding(property.Key, binding);
1111 if (null != BindingContext)
1114 xElementToNameOfOld.TryGetValue(BindingContext, out xName);
1118 var newObject = newNameScope[xName];
1119 BindingContext = newObject;
1124 internal void ClearBinding()
1126 foreach (var property in properties)
1128 if (null != property.Value.Binding)
1130 property.Value.Binding.Unapply();
1137 namespace Tizen.NUI.Binding.Internals
1140 /// SetValueFlags. For internal use.
1143 [EditorBrowsable(EditorBrowsableState.Never)]
1144 public enum SetValueFlags
1149 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1150 [EditorBrowsable(EditorBrowsableState.Never)]
1154 /// Clear OneWay bindings.
1156 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1157 [EditorBrowsable(EditorBrowsableState.Never)]
1158 ClearOneWayBindings = 1 << 0,
1161 /// Clear TwoWay bindings.
1163 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1164 [EditorBrowsable(EditorBrowsableState.Never)]
1165 ClearTwoWayBindings = 1 << 1,
1168 /// Clear dynamic resource.
1170 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1171 [EditorBrowsable(EditorBrowsableState.Never)]
1172 ClearDynamicResource = 1 << 2,
1177 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1178 [EditorBrowsable(EditorBrowsableState.Never)]
1179 RaiseOnEqual = 1 << 3