2 * Copyright(c) 2021 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;
23 using System.Runtime.CompilerServices;
24 using Tizen.NUI.Binding.Internals;
26 namespace Tizen.NUI.Binding
29 /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
31 /// <since_tizen> 9 </since_tizen>
32 public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
35 /// Implements the bound property whose interface is provided by the BindingContext property.
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public static readonly BindableProperty BindingContextProperty =
39 BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
41 var bindableObject = (BindableObject)bindable;
44 bindableObject.bindingContext = newValue;
45 bindableObject.FlushBinding();
47 if (newValue is BindableObject targetBindableObject)
49 targetBindableObject.IsCreateByXaml = true;
53 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
55 if (null != bindable.bindingContext)
57 return bindable.bindingContext;
60 if (bindable is Container container)
62 return container.Parent?.BindingContext;
70 readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
73 object inheritedContext;
75 private object bindingContext;
78 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
80 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public object BindingContext
84 get { return inheritedContext ?? GetValue(BindingContextProperty); }
85 set { SetValue(BindingContextProperty, value); }
88 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
90 SetDynamicResource(property, key, false);
94 /// Raised when a property has changed.
96 /// <since_tizen> 9 </since_tizen>
97 public event PropertyChangedEventHandler PropertyChanged;
99 /// <summary>Copy properties of other ViewStyle to this.</summary>
100 /// <param name="other">The other BindableProperty merge to this.</param>
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 public virtual void CopyFrom(BindableObject other)
104 if (null == other) return;
106 Type type1 = this.GetType();
107 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
109 Type type2 = other.GetType();
110 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
112 if (null != nameToBindableProperty1)
114 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
116 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
118 if (null != bindableProperty)
120 object value = other.GetValue(bindableProperty);
124 SetValue(keyValuePair.Value, value);
132 /// Raised whenever the BindingContext property changes.
134 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public event EventHandler BindingContextChanged;
138 internal void ClearValue(BindableProperty property, bool fromStyle)
140 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
144 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
146 /// <param name="property">The BindableProperty to clear</param>
147 internal void ClearValue(BindableProperty property)
149 ClearValue(property, fromStyle: false, checkAccess: true);
153 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
155 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
156 internal void ClearValue(BindablePropertyKey propertyKey)
158 if (propertyKey == null)
159 throw new ArgumentNullException(nameof(propertyKey));
161 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
165 /// Return true if the target property exists and has been set.
167 /// <param name="targetProperty">The target property</param>
168 /// <returns>return true if the target property exists and has been set</returns>
169 internal bool IsSet(BindableProperty targetProperty)
171 if (targetProperty == null)
172 throw new ArgumentNullException(nameof(targetProperty));
174 var bpcontext = GetContext(targetProperty);
175 return bpcontext != null
176 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
180 /// Returns the value that is contained the BindableProperty.
182 /// <param name="property">The BindableProperty for which to get the value.</param>
183 /// <returns>The value that is contained the BindableProperty</returns>
184 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
185 [EditorBrowsable(EditorBrowsableState.Never)]
186 public object GetValue(BindableProperty property)
188 if (property == null)
189 throw new ArgumentNullException(nameof(property));
191 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
194 return property.DefaultValue;
196 return context.Value;
200 /// Raised when a property is about to change.
202 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public event PropertyChangingEventHandler PropertyChanging;
207 /// Removes a previously set binding.
209 /// <param name="property">The BindableProperty from which to remove bindings.</param>
210 internal void RemoveBinding(BindableProperty property)
212 if (property == null)
213 throw new ArgumentNullException(nameof(property));
215 BindablePropertyContext context = GetContext(property);
216 if (context == null || context.Binding == null)
219 RemoveBinding(property, context);
223 /// Assigns a binding to a property.
225 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
226 /// <param name="binding">The binding to set.</param>
227 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
228 [EditorBrowsable(EditorBrowsableState.Never)]
229 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
231 SetBinding(targetProperty, binding, false);
234 private bool isCreateByXaml = false;
235 /// Only used by the IL of xaml, will never changed to not hidden.
236 [EditorBrowsable(EditorBrowsableState.Never)]
237 public virtual bool IsCreateByXaml
241 return isCreateByXaml;
245 isCreateByXaml = value;
250 /// Sets the value of the specified property.
252 /// <param name="property">The BindableProperty on which to assign a value.</param>
253 /// <param name="value">The value to set.</param>
254 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
255 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
256 [EditorBrowsable(EditorBrowsableState.Never)]
257 public void SetValue(BindableProperty property, object value)
259 if (true == isCreateByXaml)
261 SetValue(property, value, false, true);
265 if (null == property)
267 throw new ArgumentNullException(nameof(property));
269 property.PropertyChanged?.Invoke(this, null, value);
271 OnPropertyChanged(property.PropertyName);
272 OnPropertyChangedWithData(property);
276 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
278 if (property == null)
279 throw new ArgumentNullException(nameof(property));
281 if (true == isCreateByXaml)
283 if (property.IsReadOnly)
284 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
286 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
287 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
291 property.PropertyChanged?.Invoke(this, null, value);
296 /// Sets the value of the propertyKey.
298 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
299 /// <param name="value">The value to set.</param>
300 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
301 [EditorBrowsable(EditorBrowsableState.Never)]
302 public void SetValue(BindablePropertyKey propertyKey, object value)
304 if (propertyKey == null)
305 throw new ArgumentNullException(nameof(propertyKey));
307 SetValue(propertyKey.BindableProperty, value, false, false);
311 /// Set the inherited context to a neated element.
313 /// <param name="bindable">The object on which to set the inherited binding context.</param>
314 /// <param name="value">The inherited context to set.</param>
315 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
316 [EditorBrowsable(EditorBrowsableState.Never)]
317 public static void SetInheritedBindingContext(BindableObject bindable, object value)
319 if (null == bindable)
321 throw new ArgumentNullException(nameof(bindable));
324 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
325 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
328 object oldContext = bindable.inheritedContext;
330 if (ReferenceEquals(oldContext, value))
333 if (bpContext != null && oldContext == null)
334 oldContext = bpContext.Value;
336 if (bpContext != null && bpContext.Binding != null)
338 bpContext.Binding.Context = value;
339 bindable.inheritedContext = null;
343 bindable.inheritedContext = value;
346 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
347 bindable.OnBindingContextChanged();
351 /// Register the properties which can effect each other in Binding to same group.
353 [EditorBrowsable(EditorBrowsableState.Never)]
354 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
356 if (!PropertyToGroup.ContainsKey(property))
358 PropertyToGroup.Add(property, group);
361 if (null != group && !(group.Contains(property)))
367 /// Apply the bindings to BindingContext.
369 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
370 [EditorBrowsable(EditorBrowsableState.Never)]
371 protected void ApplyBindings()
373 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
377 /// Override this method to execute an action when the BindingContext changes.
379 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
380 [EditorBrowsable(EditorBrowsableState.Never)]
381 protected virtual void OnBindingContextChanged()
383 BindingContextChanged?.Invoke(this, EventArgs.Empty);
387 /// Call this method from a child class to notify that a change happened on a property.
389 /// <param name="propertyName">The name of the property that changed.</param>
390 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
391 [EditorBrowsable(EditorBrowsableState.Never)]
392 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
393 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
396 /// Call this method from a child class to notify that a change is going to happen on a property.
398 /// <param name="propertyName">The name of the property that is changing.</param>
399 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
400 [EditorBrowsable(EditorBrowsableState.Never)]
401 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
402 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
405 /// Method that is called when a bound property is changed.
407 [EditorBrowsable(EditorBrowsableState.Never)]
408 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
411 /// Unapplies all previously set bindings.
413 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
414 [EditorBrowsable(EditorBrowsableState.Never)]
415 protected void UnapplyBindings()
417 for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
419 BindablePropertyContext context = properties[i];
420 if (context.Binding == null)
423 context.Binding.Unapply();
427 internal bool GetIsBound(BindableProperty targetProperty)
429 if (targetProperty == null)
430 throw new ArgumentNullException(nameof(targetProperty));
432 BindablePropertyContext bpcontext = GetContext(targetProperty);
433 return bpcontext != null && bpcontext.Binding != null;
437 /// Returns the value that is contained the BindableProperty.
439 /// <param name="property0">The BindableProperty instance.</param>
440 /// <param name="property1">The BindableProperty instance.</param>
441 /// <returns>The value that is contained the BindableProperty</returns>
442 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
444 var values = new object[2];
446 for (var i = 0; i < properties.Count; i++)
448 BindablePropertyContext context = properties[i];
450 if (ReferenceEquals(context.Property, property0))
452 values[0] = context.Value;
455 else if (ReferenceEquals(context.Property, property1))
457 values[1] = context.Value;
461 if (property0 == null && property1 == null)
465 if (!ReferenceEquals(property0, null))
466 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
467 if (!ReferenceEquals(property1, null))
468 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
474 /// Returns the value that is contained the BindableProperty.
476 /// <param name="property0">The BindableProperty instance.</param>
477 /// <param name="property1">The BindableProperty instance.</param>
478 /// <param name="property2">The BindableProperty instance.</param>
479 /// <returns>The value that is contained the BindableProperty</returns>
480 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
482 var values = new object[3];
484 for (var i = 0; i < properties.Count; i++)
486 BindablePropertyContext context = properties[i];
488 if (ReferenceEquals(context.Property, property0))
490 values[0] = context.Value;
493 else if (ReferenceEquals(context.Property, property1))
495 values[1] = context.Value;
498 else if (ReferenceEquals(context.Property, property2))
500 values[2] = context.Value;
504 if (property0 == null && property1 == null && property2 == null)
508 if (!ReferenceEquals(property0, null))
509 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
510 if (!ReferenceEquals(property1, null))
511 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
512 if (!ReferenceEquals(property2, null))
513 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
519 /// Returns the value that is contained the BindableProperty.
521 /// <param name="properties">The array of the BindableProperty instances</param>
522 /// <returns>The values that is contained the BindableProperty instances.</returns>
523 internal object[] GetValues(params BindableProperty[] properties)
525 var values = new object[properties.Length];
526 for (var i = 0; i < this.properties.Count; i++)
528 var context = this.properties[i];
529 var index = properties.IndexOf(context.Property);
532 values[index] = context.Value;
534 for (var i = 0; i < values.Length; i++)
536 if (!ReferenceEquals(values[i], null))
538 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
543 internal virtual void OnRemoveDynamicResource(BindableProperty property)
547 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
551 internal void RemoveDynamicResource(BindableProperty property)
553 if (property == null)
554 throw new ArgumentNullException(nameof(property));
556 OnRemoveDynamicResource(property);
557 BindablePropertyContext context = GetOrCreateContext(property);
558 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
561 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
563 if (targetProperty == null)
564 throw new ArgumentNullException(nameof(targetProperty));
566 throw new ArgumentNullException(nameof(binding));
568 if (fromStyle && !CanBeSetFromStyle(targetProperty))
571 IsCreateByXaml = true;
573 var context = GetOrCreateContext(targetProperty);
575 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
577 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
579 if (context.Binding != null)
580 context.Binding.Unapply();
582 BindingBase oldBinding = context.Binding;
583 context.Binding = binding;
585 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
587 binding.Apply(BindingContext, this, targetProperty);
590 bool CanBeSetFromStyle(BindableProperty property)
592 var context = GetContext(property);
595 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
597 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
599 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
604 internal void SetDynamicResource(BindableProperty property, string key)
606 SetDynamicResource(property, key, false);
609 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
611 if (property == null)
612 throw new ArgumentNullException(nameof(property));
613 if (string.IsNullOrEmpty(key))
614 throw new ArgumentNullException(nameof(key));
615 if (fromStyle && !CanBeSetFromStyle(property))
618 var context = GetOrCreateContext(property);
620 context.Attributes |= BindableContextAttributes.IsDynamicResource;
622 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
624 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
626 OnSetDynamicResource(property, key);
629 internal void SetValue(BindableProperty property, object value, bool fromStyle)
631 SetValue(property, value, fromStyle, true);
634 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
636 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
640 /// For internal use.
642 /// <param name="property">The BindableProperty on which to assign a value.</param>
643 /// <param name="value">The value to set</param>
644 /// <param name="attributes">The set value flag</param>
645 [EditorBrowsable(EditorBrowsableState.Never)]
646 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
648 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
651 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
653 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
654 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
655 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
656 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
657 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
659 if (property == null)
660 throw new ArgumentNullException(nameof(property));
661 if (checkAccess && property.IsReadOnly)
663 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
667 if (!converted && !property.TryConvert(ref value))
669 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
673 if (property.ValidateValue != null && !property.ValidateValue(this, value))
674 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
676 if (property.CoerceValue != null)
677 value = property.CoerceValue(this, value);
679 BindablePropertyContext context = GetOrCreateContext(property);
682 context.Attributes |= BindableContextAttributes.IsManuallySet;
683 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
687 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
692 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
694 // else omitted on purpose
696 bool currentlyApplying = applying;
698 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
700 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
701 if (delayQueue == null)
702 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
704 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
708 context.Attributes |= BindableContextAttributes.IsBeingSet;
709 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
711 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
712 if (delayQueue != null)
714 while (delayQueue.Count > 0)
716 SetValueArgs s = delayQueue.Dequeue();
717 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
720 context.DelayedSetters = null;
723 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
727 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
729 var prop = properties.ToArray();
730 for (int i = 0, propLength = prop.Length; i < propLength; i++)
732 BindablePropertyContext context = prop[i];
733 BindingBase binding = context.Binding;
737 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
740 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
741 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
745 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
747 object context = bindable.inheritedContext;
748 var oldBinding = oldBindingBase as Binding;
749 var newBinding = newBindingBase as Binding;
751 if (context == null && oldBinding != null)
752 context = oldBinding.Context;
753 if (context != null && newBinding != null)
754 newBinding.Context = context;
757 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
759 if (property == null)
760 throw new ArgumentNullException(nameof(property));
762 if (checkAccess && property.IsReadOnly)
763 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
765 BindablePropertyContext bpcontext = GetContext(property);
766 if (bpcontext == null)
769 if (fromStyle && !CanBeSetFromStyle(property))
772 object original = bpcontext.Value;
774 object newValue = property.GetDefaultValue(this);
776 bool same = Equals(original, newValue);
779 property.PropertyChanging?.Invoke(this, original, newValue);
781 OnPropertyChanging(property.PropertyName);
784 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
785 bpcontext.Value = newValue;
786 if (property.DefaultValueCreator == null)
787 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
789 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
793 OnPropertyChanged(property.PropertyName);
794 OnPropertyChangedWithData(property);
795 property.PropertyChanged?.Invoke(this, original, newValue);
799 [MethodImpl(MethodImplOptions.AggressiveInlining)]
800 BindablePropertyContext CreateAndAddContext(BindableProperty property)
802 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
804 if (property.DefaultValueCreator == null)
805 context.Attributes = BindableContextAttributes.IsDefaultValue;
807 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
809 properties.Add(context);
813 [MethodImpl(MethodImplOptions.AggressiveInlining)]
814 BindablePropertyContext GetContext(BindableProperty property)
816 List<BindablePropertyContext> propertyList = properties;
818 for (var i = 0; i < propertyList.Count; i++)
820 BindablePropertyContext context = propertyList[i];
821 if (ReferenceEquals(context.Property, property))
828 [MethodImpl(MethodImplOptions.AggressiveInlining)]
829 BindablePropertyContext GetOrCreateContext(BindableProperty property)
831 BindablePropertyContext context = GetContext(property);
834 context = CreateAndAddContext(property);
836 else if (property.DefaultValueCreator != null)
838 context.Value = property.DefaultValueCreator(this); //Update Value from dali
844 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
846 context.Binding.Unapply();
848 property.BindingChanging?.Invoke(this, context.Binding, null);
850 context.Binding = null;
853 internal void SetValue(BindableProperty property, object value, 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 if (fromStyle && !CanBeSetFromStyle(property))
864 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
865 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
869 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
871 object original = context.Value;
872 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
873 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
874 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
875 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
877 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
878 if (!silent && (!same || raiseOnEqual))
880 property.PropertyChanging?.Invoke(this, original, value);
882 OnPropertyChanging(property.PropertyName);
885 if (!same || raiseOnEqual)
887 context.Value = value;
890 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
891 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
893 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
894 RemoveDynamicResource(property);
896 BindingBase binding = context.Binding;
899 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
901 RemoveBinding(property, context);
906 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
910 if ((!same || raiseOnEqual))
912 property.PropertyChanged?.Invoke(this, original, value);
914 if (binding != null && !currentlyApplying)
921 OnPropertyChanged(property.PropertyName);
923 if (null != propertyGroup)
925 foreach (var relativeProperty in propertyGroup)
927 if (relativeProperty != property)
929 var relativeContext = GetOrCreateContext(relativeProperty);
930 var relativeBinding = relativeContext.Binding;
932 if (null != relativeBinding)
935 relativeBinding.Apply(true);
939 OnPropertyChanged(relativeProperty.PropertyName);
944 else if (true == same && true == forceSendChangeSignal)
946 if (binding != null && !currentlyApplying)
953 OnPropertyChanged(property.PropertyName);
955 if (null != propertyGroup)
957 foreach (var relativeProperty in propertyGroup)
959 if (relativeProperty != property)
961 var relativeContext = GetOrCreateContext(relativeProperty);
962 var relativeBinding = relativeContext.Binding;
964 if (null != relativeBinding)
967 relativeBinding.Apply(true);
971 OnPropertyChanged(relativeProperty.PropertyName);
977 OnPropertyChangedWithData(property);
981 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
982 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
985 enum BindableContextAttributes
987 IsManuallySet = 1 << 0,
989 IsDynamicResource = 1 << 2,
990 IsSetFromStyle = 1 << 3,
991 IsDefaultValue = 1 << 4,
992 IsDefaultValueCreated = 1 << 5,
995 class BindablePropertyContext
997 public BindableContextAttributes Attributes;
998 public BindingBase Binding;
999 public Queue<SetValueArgs> DelayedSetters;
1000 public BindableProperty Property;
1001 public object Value;
1005 internal enum SetValuePrivateFlags
1008 CheckAccess = 1 << 0,
1010 ManuallySet = 1 << 2,
1013 Default = CheckAccess
1018 public readonly SetValueFlags Attributes;
1019 public readonly BindablePropertyContext Context;
1020 public readonly bool CurrentlyApplying;
1021 public readonly BindableProperty Property;
1022 public readonly object Value;
1024 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1026 Property = property;
1029 CurrentlyApplying = currentlyApplying;
1030 Attributes = attributes;
1034 internal void AddChildBindableObject(BindableObject child)
1038 children.Add(child);
1039 child.FlushBinding();
1043 internal void RemoveChildBindableObject(BindableObject child)
1045 children.Remove(child);
1048 private List<BindableObject> children = new List<BindableObject>();
1050 private void FlushBinding()
1052 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1053 OnBindingContextChanged();
1055 foreach (var child in children)
1057 child.FlushBinding();
1063 namespace Tizen.NUI.Binding.Internals
1066 /// SetValueFlags. For internal use.
1069 [EditorBrowsable(EditorBrowsableState.Never)]
1070 public enum SetValueFlags
1075 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1076 [EditorBrowsable(EditorBrowsableState.Never)]
1080 /// Clear OneWay bindings.
1082 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1083 [EditorBrowsable(EditorBrowsableState.Never)]
1084 ClearOneWayBindings = 1 << 0,
1087 /// Clear TwoWay bindings.
1089 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1090 [EditorBrowsable(EditorBrowsableState.Never)]
1091 ClearTwoWayBindings = 1 << 1,
1094 /// Clear dynamic resource.
1096 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1097 [EditorBrowsable(EditorBrowsableState.Never)]
1098 ClearDynamicResource = 1 << 2,
1103 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1104 [EditorBrowsable(EditorBrowsableState.Never)]
1105 RaiseOnEqual = 1 << 3