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 inheritedContext ?? GetValue(BindingContextProperty); }
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 BindablePropertyContext context = GetOrCreateContext(property);
329 context.Attributes |= BindableContextAttributes.IsManuallySet;
331 object oldvalue = null;
332 if (null == property.DefaultValueCreator)
334 oldvalue = context.Value;
335 context.Value = value;
339 oldvalue = property.DefaultValueCreator.Invoke(this);
342 property.PropertyChanged?.Invoke(this, oldvalue, value);
344 OnPropertyChanged(property.PropertyName);
345 OnPropertyChangedWithData(property);
348 ChangedPropertiesSet.Add(property);
351 private HashSet<BindableProperty> changedPropertiesSet;
352 private HashSet<BindableProperty> ChangedPropertiesSet
356 if (null == changedPropertiesSet)
358 changedPropertiesSet = new HashSet<BindableProperty>();
361 return changedPropertiesSet;
365 private HashSet<BindableProperty> changedPropertiesSetExcludingStyle;
366 internal protected HashSet<BindableProperty> ChangedPropertiesSetExcludingStyle
370 if (null == changedPropertiesSetExcludingStyle)
372 changedPropertiesSetExcludingStyle = new HashSet<BindableProperty>();
375 return changedPropertiesSetExcludingStyle;
380 /// Sets the value of the propertyKey.
382 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
383 /// <param name="value">The value to set.</param>
384 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
385 [EditorBrowsable(EditorBrowsableState.Never)]
386 public void SetValue(BindablePropertyKey propertyKey, object value)
388 if (propertyKey == null)
389 throw new ArgumentNullException(nameof(propertyKey));
391 SetValue(propertyKey.BindableProperty, value, false, false);
395 /// Set the inherited context to a neated element.
397 /// <param name="bindable">The object on which to set the inherited binding context.</param>
398 /// <param name="value">The inherited context to set.</param>
399 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
400 [EditorBrowsable(EditorBrowsableState.Never)]
401 public static void SetInheritedBindingContext(BindableObject bindable, object value)
403 if (null == bindable)
405 throw new ArgumentNullException(nameof(bindable));
408 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
409 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
412 object oldContext = bindable.inheritedContext;
414 if (ReferenceEquals(oldContext, value))
417 if (bpContext != null && oldContext == null)
418 oldContext = bpContext.Value;
420 if (bpContext != null && bpContext.Binding != null)
422 bpContext.Binding.Context = value;
423 bindable.inheritedContext = null;
427 bindable.inheritedContext = value;
430 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
431 bindable.OnBindingContextChanged();
435 /// Register the properties which can effect each other in Binding to same group.
437 [EditorBrowsable(EditorBrowsableState.Never)]
438 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
440 if (!PropertyToGroup.ContainsKey(property))
442 PropertyToGroup.Add(property, group);
445 if (null != group && !(group.Contains(property)))
451 /// Apply the bindings to BindingContext.
453 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
454 [EditorBrowsable(EditorBrowsableState.Never)]
455 protected void ApplyBindings()
457 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
461 /// Override this method to execute an action when the BindingContext changes.
463 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
464 [EditorBrowsable(EditorBrowsableState.Never)]
465 protected virtual void OnBindingContextChanged()
467 BindingContextChanged?.Invoke(this, EventArgs.Empty);
471 /// Call this method from a child class to notify that a change happened on a property.
473 /// <param name="propertyName">The name of the property that changed.</param>
474 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
475 [EditorBrowsable(EditorBrowsableState.Never)]
476 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
477 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
480 /// Call this method from a child class to notify that a change is going to happen on a property.
482 /// <param name="propertyName">The name of the property that is changing.</param>
483 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
484 [EditorBrowsable(EditorBrowsableState.Never)]
485 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
486 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
489 /// Method that is called when a bound property is changed.
491 [EditorBrowsable(EditorBrowsableState.Never)]
492 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
495 /// Unapplies all previously set bindings.
497 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
498 [EditorBrowsable(EditorBrowsableState.Never)]
499 protected void UnapplyBindings()
501 foreach (var context in properties.Values)
503 if (context.Binding == null)
506 context.Binding.Unapply();
510 internal bool GetIsBound(BindableProperty targetProperty)
512 if (targetProperty == null)
513 throw new ArgumentNullException(nameof(targetProperty));
515 BindablePropertyContext bpcontext = GetContext(targetProperty);
516 return bpcontext != null && bpcontext.Binding != null;
520 /// Returns the value that is contained the BindableProperty.
522 /// <param name="property0">The BindableProperty instance.</param>
523 /// <param name="property1">The BindableProperty instance.</param>
524 /// <returns>The value that is contained the BindableProperty</returns>
525 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
527 var values = new object[2];
529 foreach (var context in properties.Values)
531 if (ReferenceEquals(context.Property, property0))
533 values[0] = context.Value;
536 else if (ReferenceEquals(context.Property, property1))
538 values[1] = context.Value;
542 if (property0 == null && property1 == null)
546 if (!ReferenceEquals(property0, null))
547 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
548 if (!ReferenceEquals(property1, null))
549 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
555 /// Returns the value that is contained the BindableProperty.
557 /// <param name="property0">The BindableProperty instance.</param>
558 /// <param name="property1">The BindableProperty instance.</param>
559 /// <param name="property2">The BindableProperty instance.</param>
560 /// <returns>The value that is contained the BindableProperty</returns>
561 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
563 var values = new object[3];
565 foreach (var context in properties.Values)
567 if (ReferenceEquals(context.Property, property0))
569 values[0] = context.Value;
572 else if (ReferenceEquals(context.Property, property1))
574 values[1] = context.Value;
577 else if (ReferenceEquals(context.Property, property2))
579 values[2] = context.Value;
583 if (property0 == null && property1 == null && property2 == null)
587 if (!ReferenceEquals(property0, null))
588 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
589 if (!ReferenceEquals(property1, null))
590 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
591 if (!ReferenceEquals(property2, null))
592 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
598 /// Returns the value that is contained the BindableProperty.
600 /// <param name="properties">The array of the BindableProperty instances</param>
601 /// <returns>The values that is contained the BindableProperty instances.</returns>
602 internal object[] GetValues(params BindableProperty[] properties)
604 var values = new object[properties.Length];
605 foreach (var context in this.properties.Values)
607 var index = properties.IndexOf(context.Property);
610 values[index] = context.Value;
612 for (var i = 0; i < values.Length; i++)
614 if (!ReferenceEquals(values[i], null))
616 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
621 internal virtual void OnRemoveDynamicResource(BindableProperty property)
625 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
629 internal void RemoveDynamicResource(BindableProperty property)
631 if (property == null)
632 throw new ArgumentNullException(nameof(property));
634 OnRemoveDynamicResource(property);
635 BindablePropertyContext context = GetOrCreateContext(property);
636 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
639 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
641 if (targetProperty == null)
642 throw new ArgumentNullException(nameof(targetProperty));
644 throw new ArgumentNullException(nameof(binding));
646 if (fromStyle && !CanBeSetFromStyle(targetProperty))
651 var context = GetOrCreateContext(targetProperty);
653 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
655 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
657 if (context.Binding != null)
658 context.Binding.Unapply();
660 BindingBase oldBinding = context.Binding;
661 context.Binding = binding;
663 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
665 binding.Apply(BindingContext, this, targetProperty);
668 bool CanBeSetFromStyle(BindableProperty property)
670 var context = GetContext(property);
673 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
675 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
677 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
682 internal void SetDynamicResource(BindableProperty property, string key)
684 SetDynamicResource(property, key, false);
687 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
689 if (property == null)
690 throw new ArgumentNullException(nameof(property));
691 if (string.IsNullOrEmpty(key))
692 throw new ArgumentNullException(nameof(key));
693 if (fromStyle && !CanBeSetFromStyle(property))
696 var context = GetOrCreateContext(property);
698 context.Attributes |= BindableContextAttributes.IsDynamicResource;
700 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
702 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
704 OnSetDynamicResource(property, key);
707 internal void SetValue(BindableProperty property, object value, bool fromStyle)
709 SetValue(property, value, fromStyle, true);
712 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
714 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None);
718 /// For internal use.
720 /// <param name="property">The BindableProperty on which to assign a value.</param>
721 /// <param name="value">The value to set</param>
722 /// <param name="attributes">The set value flag</param>
723 [EditorBrowsable(EditorBrowsableState.Never)]
724 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
726 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default);
729 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
731 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
732 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
733 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
734 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
735 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
737 if (property == null)
738 throw new ArgumentNullException(nameof(property));
739 if (checkAccess && property.IsReadOnly)
741 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
745 if (!converted && !property.TryConvert(ref value))
747 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
751 if (property.ValidateValue != null && !property.ValidateValue(this, value))
752 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
754 if (property.CoerceValue != null)
755 value = property.CoerceValue(this, value);
757 BindablePropertyContext context = GetOrCreateContext(property);
760 context.Attributes |= BindableContextAttributes.IsManuallySet;
761 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
765 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
770 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
772 // else omitted on purpose
774 bool currentlyApplying = applying;
776 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
778 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
779 if (delayQueue == null)
780 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
782 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
786 context.Attributes |= BindableContextAttributes.IsBeingSet;
787 SetValueActual(property, context, value, currentlyApplying, attributes, silent);
789 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
790 if (delayQueue != null)
792 while (delayQueue.Count > 0)
794 SetValueArgs s = delayQueue.Dequeue();
795 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, s.Attributes);
798 context.DelayedSetters = null;
801 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
805 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
807 var prop = properties.Values.ToArray();
808 for (int i = 0, propLength = prop.Length; i < propLength; i++)
810 BindablePropertyContext context = prop[i];
811 BindingBase binding = context.Binding;
815 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
818 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
819 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
823 internal bool IsBinded
829 static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
831 bindable.inheritedContext = null;
832 bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
833 bindable.OnBindingContextChanged();
835 if (newvalue is BindableObject targetBindableObject)
837 targetBindableObject.IsBinded = true;
841 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
843 object context = bindable.inheritedContext;
844 var oldBinding = oldBindingBase as Binding;
845 var newBinding = newBindingBase as Binding;
847 if (context == null && oldBinding != null)
848 context = oldBinding.Context;
849 if (context != null && newBinding != null)
850 newBinding.Context = context;
853 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
855 if (property == null)
856 throw new ArgumentNullException(nameof(property));
858 if (checkAccess && property.IsReadOnly)
859 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
861 BindablePropertyContext bpcontext = GetContext(property);
862 if (bpcontext == null)
865 if (fromStyle && !CanBeSetFromStyle(property))
868 object original = bpcontext.Value;
870 object newValue = property.GetDefaultValue(this);
872 bool same = Equals(original, newValue);
875 property.PropertyChanging?.Invoke(this, original, newValue);
877 OnPropertyChanging(property.PropertyName);
880 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
881 bpcontext.Value = newValue;
882 if (property.DefaultValueCreator == null)
883 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
885 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
889 OnPropertyChanged(property.PropertyName);
890 OnPropertyChangedWithData(property);
891 property.PropertyChanged?.Invoke(this, original, newValue);
895 [MethodImpl(MethodImplOptions.AggressiveInlining)]
896 BindablePropertyContext CreateAndAddContext(BindableProperty property)
898 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
900 if (property.DefaultValueCreator == null)
901 context.Attributes = BindableContextAttributes.IsDefaultValue;
903 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
905 properties.Add(property, context);
909 [MethodImpl(MethodImplOptions.AggressiveInlining)]
910 BindablePropertyContext GetContext(BindableProperty property) => properties.TryGetValue(property, out var result) ? result : null;
912 [MethodImpl(MethodImplOptions.AggressiveInlining)]
913 BindablePropertyContext GetOrCreateContext(BindableProperty property)
915 BindablePropertyContext context = GetContext(property);
918 context = CreateAndAddContext(property);
920 else if (property.ValueGetter != null)
922 context.Value = property.ValueGetter(this); //Update Value from dali
923 }//added by xiaohui.fang
924 else if (property.DefaultValueCreator != null) //This will be removed in the future.
926 context.Value = property.DefaultValueCreator(this); //Update Value from dali
932 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
934 context.Binding.Unapply();
936 property.BindingChanging?.Invoke(this, context.Binding, null);
938 context.Binding = null;
941 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
943 if (property == null)
944 throw new ArgumentNullException(nameof(property));
946 if (checkAccess && property.IsReadOnly)
947 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
949 if (fromStyle && !CanBeSetFromStyle(property))
952 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
953 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0));
956 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, bool silent = false)
958 object original = context.Value;
959 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
960 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
961 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
962 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
964 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
965 if (!silent && (!same || raiseOnEqual))
967 property.PropertyChanging?.Invoke(this, original, value);
969 OnPropertyChanging(property.PropertyName);
972 if (!same || raiseOnEqual)
974 context.Value = value;
977 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
978 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
980 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
981 RemoveDynamicResource(property);
983 BindingBase binding = context.Binding;
986 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
988 RemoveBinding(property, context);
993 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
995 if (!silent && (!same || raiseOnEqual))
997 property.PropertyChanged?.Invoke(this, original, value);
999 if (binding != null && !currentlyApplying)
1002 binding.Apply(true);
1006 OnPropertyChanged(property.PropertyName);
1008 if (null != propertyGroup)
1010 foreach (var relativeProperty in propertyGroup)
1012 if (relativeProperty != property)
1014 var relativeContext = GetOrCreateContext(relativeProperty);
1015 var relativeBinding = relativeContext.Binding;
1017 if (null != relativeBinding)
1020 relativeBinding.Apply(true);
1024 OnPropertyChanged(relativeProperty.PropertyName);
1029 OnPropertyChangedWithData(property);
1033 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
1034 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1037 enum BindableContextAttributes
1039 IsManuallySet = 1 << 0,
1040 IsBeingSet = 1 << 1,
1041 IsDynamicResource = 1 << 2,
1042 IsSetFromStyle = 1 << 3,
1043 IsDefaultValue = 1 << 4,
1044 IsDefaultValueCreated = 1 << 5,
1047 class BindablePropertyContext
1049 public BindableContextAttributes Attributes;
1050 public BindingBase Binding;
1051 public Queue<SetValueArgs> DelayedSetters;
1052 public BindableProperty Property;
1053 public object Value;
1057 internal enum SetValuePrivateFlags
1060 CheckAccess = 1 << 0,
1062 ManuallySet = 1 << 2,
1065 Default = CheckAccess
1070 public readonly SetValueFlags Attributes;
1071 public readonly BindablePropertyContext Context;
1072 public readonly bool CurrentlyApplying;
1073 public readonly BindableProperty Property;
1074 public readonly object Value;
1076 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1078 Property = property;
1081 CurrentlyApplying = currentlyApplying;
1082 Attributes = attributes;
1086 internal void ReplaceBindingElement(Dictionary<string, object> oldNameScope, Dictionary<string, object> newNameScope)
1088 var xElementToNameOfOld = new Dictionary<object, string>();
1090 foreach (var pair in oldNameScope)
1092 xElementToNameOfOld.Add(pair.Value, pair.Key);
1095 foreach (var property in properties)
1097 if (property.Value.Binding is Binding binding && null != binding.Source)
1100 xElementToNameOfOld.TryGetValue(binding.Source, out xName);
1104 var newObject = newNameScope[xName];
1106 binding.Source = newObject;
1107 SetBinding(property.Key, binding);
1112 if (null != BindingContext)
1115 xElementToNameOfOld.TryGetValue(BindingContext, out xName);
1119 var newObject = newNameScope[xName];
1120 BindingContext = newObject;
1125 internal void ClearBinding()
1127 foreach (var property in properties)
1129 if (null != property.Value.Binding)
1131 property.Value.Binding.Unapply();
1138 namespace Tizen.NUI.Binding.Internals
1141 /// SetValueFlags. For internal use.
1144 [EditorBrowsable(EditorBrowsableState.Never)]
1145 public enum SetValueFlags
1150 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1151 [EditorBrowsable(EditorBrowsableState.Never)]
1155 /// Clear OneWay bindings.
1157 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1158 [EditorBrowsable(EditorBrowsableState.Never)]
1159 ClearOneWayBindings = 1 << 0,
1162 /// Clear TwoWay bindings.
1164 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1165 [EditorBrowsable(EditorBrowsableState.Never)]
1166 ClearTwoWayBindings = 1 << 1,
1169 /// Clear dynamic resource.
1171 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1172 [EditorBrowsable(EditorBrowsableState.Never)]
1173 ClearDynamicResource = 1 << 2,
1178 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1179 [EditorBrowsable(EditorBrowsableState.Never)]
1180 RaiseOnEqual = 1 << 3