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 && (SettedPropeties.Contains(bindableProperty) || other.SettedPropeties.Contains(bindableProperty)))
99 object value = other.GetValue(bindableProperty);
103 SetValue(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 private bool isCreateByXaml = false;
247 /// Only used by the IL of xaml, will never changed to not hidden.
248 [EditorBrowsable(EditorBrowsableState.Never)]
249 public virtual bool IsCreateByXaml
253 return isCreateByXaml;
257 isCreateByXaml = value;
262 /// Sets the value of the specified property.
264 /// <param name="property">The BindableProperty on which to assign a value.</param>
265 /// <param name="value">The value to set.</param>
266 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
267 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
268 [EditorBrowsable(EditorBrowsableState.Never)]
269 public void SetValue(BindableProperty property, object value)
271 if (true == IsBinded)
273 SetValue(property, value, false, true);
277 if (null == property)
279 throw new ArgumentNullException(nameof(property));
282 object oldvalue = null;
283 if (null == property.DefaultValueCreator)
285 BindablePropertyContext context = GetOrCreateContext(property);
288 oldvalue = context.Value;
289 context.Value = value;
294 oldvalue = property.DefaultValueCreator.Invoke(this);
297 property.PropertyChanged?.Invoke(this, oldvalue, value);
299 OnPropertyChanged(property.PropertyName);
300 OnPropertyChangedWithData(property);
303 SettedPropeties.Add(property);
306 private HashSet<BindableProperty> settedPropeties;
307 private HashSet<BindableProperty> SettedPropeties
311 if (null == settedPropeties)
313 settedPropeties = new HashSet<BindableProperty>();
316 return settedPropeties;
320 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
322 if (property == null)
323 throw new ArgumentNullException(nameof(property));
325 if (true == isCreateByXaml)
327 if (property.IsReadOnly)
328 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
330 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
331 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
335 property.PropertyChanged?.Invoke(this, null, value);
340 /// Sets the value of the propertyKey.
342 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
343 /// <param name="value">The value to set.</param>
344 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
345 [EditorBrowsable(EditorBrowsableState.Never)]
346 public void SetValue(BindablePropertyKey propertyKey, object value)
348 if (propertyKey == null)
349 throw new ArgumentNullException(nameof(propertyKey));
351 SetValue(propertyKey.BindableProperty, value, false, false);
355 /// Set the inherited context to a neated element.
357 /// <param name="bindable">The object on which to set the inherited binding context.</param>
358 /// <param name="value">The inherited context to set.</param>
359 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
360 [EditorBrowsable(EditorBrowsableState.Never)]
361 public static void SetInheritedBindingContext(BindableObject bindable, object value)
363 if (null == bindable)
365 throw new ArgumentNullException(nameof(bindable));
368 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
369 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
372 object oldContext = bindable.inheritedContext;
374 if (ReferenceEquals(oldContext, value))
377 if (bpContext != null && oldContext == null)
378 oldContext = bpContext.Value;
380 if (bpContext != null && bpContext.Binding != null)
382 bpContext.Binding.Context = value;
383 bindable.inheritedContext = null;
387 bindable.inheritedContext = value;
390 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
391 bindable.OnBindingContextChanged();
395 /// Register the properties which can effect each other in Binding to same group.
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
400 if (!PropertyToGroup.ContainsKey(property))
402 PropertyToGroup.Add(property, group);
405 if (null != group && !(group.Contains(property)))
411 /// Apply the bindings to BindingContext.
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 ApplyBindings()
417 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
421 /// Override this method to execute an action when the BindingContext changes.
423 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
424 [EditorBrowsable(EditorBrowsableState.Never)]
425 protected virtual void OnBindingContextChanged()
427 BindingContextChanged?.Invoke(this, EventArgs.Empty);
431 /// Call this method from a child class to notify that a change happened on a property.
433 /// <param name="propertyName">The name of the property that changed.</param>
434 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
435 [EditorBrowsable(EditorBrowsableState.Never)]
436 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
437 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
440 /// Call this method from a child class to notify that a change is going to happen on a property.
442 /// <param name="propertyName">The name of the property that is changing.</param>
443 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
444 [EditorBrowsable(EditorBrowsableState.Never)]
445 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
446 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
449 /// Method that is called when a bound property is changed.
451 [EditorBrowsable(EditorBrowsableState.Never)]
452 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
455 /// Unapplies all previously set bindings.
457 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
458 [EditorBrowsable(EditorBrowsableState.Never)]
459 protected void UnapplyBindings()
461 foreach (var context in properties.Values)
463 if (context.Binding == null)
466 context.Binding.Unapply();
470 internal bool GetIsBound(BindableProperty targetProperty)
472 if (targetProperty == null)
473 throw new ArgumentNullException(nameof(targetProperty));
475 BindablePropertyContext bpcontext = GetContext(targetProperty);
476 return bpcontext != null && bpcontext.Binding != null;
480 /// Returns the value that is contained the BindableProperty.
482 /// <param name="property0">The BindableProperty instance.</param>
483 /// <param name="property1">The BindableProperty instance.</param>
484 /// <returns>The value that is contained the BindableProperty</returns>
485 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
487 var values = new object[2];
489 foreach (var context in properties.Values)
491 if (ReferenceEquals(context.Property, property0))
493 values[0] = context.Value;
496 else if (ReferenceEquals(context.Property, property1))
498 values[1] = context.Value;
502 if (property0 == null && property1 == null)
506 if (!ReferenceEquals(property0, null))
507 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
508 if (!ReferenceEquals(property1, null))
509 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
515 /// Returns the value that is contained the BindableProperty.
517 /// <param name="property0">The BindableProperty instance.</param>
518 /// <param name="property1">The BindableProperty instance.</param>
519 /// <param name="property2">The BindableProperty instance.</param>
520 /// <returns>The value that is contained the BindableProperty</returns>
521 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
523 var values = new object[3];
525 foreach (var context in properties.Values)
527 if (ReferenceEquals(context.Property, property0))
529 values[0] = context.Value;
532 else if (ReferenceEquals(context.Property, property1))
534 values[1] = context.Value;
537 else if (ReferenceEquals(context.Property, property2))
539 values[2] = context.Value;
543 if (property0 == null && property1 == null && property2 == null)
547 if (!ReferenceEquals(property0, null))
548 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
549 if (!ReferenceEquals(property1, null))
550 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
551 if (!ReferenceEquals(property2, null))
552 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
558 /// Returns the value that is contained the BindableProperty.
560 /// <param name="properties">The array of the BindableProperty instances</param>
561 /// <returns>The values that is contained the BindableProperty instances.</returns>
562 internal object[] GetValues(params BindableProperty[] properties)
564 var values = new object[properties.Length];
565 foreach (var context in this.properties.Values)
567 var index = properties.IndexOf(context.Property);
570 values[index] = context.Value;
572 for (var i = 0; i < values.Length; i++)
574 if (!ReferenceEquals(values[i], null))
576 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
581 internal virtual void OnRemoveDynamicResource(BindableProperty property)
585 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
589 internal void RemoveDynamicResource(BindableProperty property)
591 if (property == null)
592 throw new ArgumentNullException(nameof(property));
594 OnRemoveDynamicResource(property);
595 BindablePropertyContext context = GetOrCreateContext(property);
596 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
599 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
601 if (targetProperty == null)
602 throw new ArgumentNullException(nameof(targetProperty));
604 throw new ArgumentNullException(nameof(binding));
606 if (fromStyle && !CanBeSetFromStyle(targetProperty))
611 var context = GetOrCreateContext(targetProperty);
613 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
615 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
617 if (context.Binding != null)
618 context.Binding.Unapply();
620 BindingBase oldBinding = context.Binding;
621 context.Binding = binding;
623 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
625 binding.Apply(BindingContext, this, targetProperty);
628 bool CanBeSetFromStyle(BindableProperty property)
630 var context = GetContext(property);
633 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
635 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
637 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
642 internal void SetDynamicResource(BindableProperty property, string key)
644 SetDynamicResource(property, key, false);
647 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
649 if (property == null)
650 throw new ArgumentNullException(nameof(property));
651 if (string.IsNullOrEmpty(key))
652 throw new ArgumentNullException(nameof(key));
653 if (fromStyle && !CanBeSetFromStyle(property))
656 var context = GetOrCreateContext(property);
658 context.Attributes |= BindableContextAttributes.IsDynamicResource;
660 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
662 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
664 OnSetDynamicResource(property, key);
667 internal void SetValue(BindableProperty property, object value, bool fromStyle)
669 SetValue(property, value, fromStyle, true);
672 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
674 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
678 /// For internal use.
680 /// <param name="property">The BindableProperty on which to assign a value.</param>
681 /// <param name="value">The value to set</param>
682 /// <param name="attributes">The set value flag</param>
683 [EditorBrowsable(EditorBrowsableState.Never)]
684 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
686 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
689 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
691 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
692 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
693 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
694 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
695 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
697 if (property == null)
698 throw new ArgumentNullException(nameof(property));
699 if (checkAccess && property.IsReadOnly)
701 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
705 if (!converted && !property.TryConvert(ref value))
707 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
711 if (property.ValidateValue != null && !property.ValidateValue(this, value))
712 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
714 if (property.CoerceValue != null)
715 value = property.CoerceValue(this, value);
717 BindablePropertyContext context = GetOrCreateContext(property);
720 context.Attributes |= BindableContextAttributes.IsManuallySet;
721 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
725 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
730 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
732 // else omitted on purpose
734 bool currentlyApplying = applying;
736 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
738 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
739 if (delayQueue == null)
740 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
742 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
746 context.Attributes |= BindableContextAttributes.IsBeingSet;
747 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
749 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
750 if (delayQueue != null)
752 while (delayQueue.Count > 0)
754 SetValueArgs s = delayQueue.Dequeue();
755 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
758 context.DelayedSetters = null;
761 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
765 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
767 var prop = properties.Values.ToArray();
768 for (int i = 0, propLength = prop.Length; i < propLength; i++)
770 BindablePropertyContext context = prop[i];
771 BindingBase binding = context.Binding;
775 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
778 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
779 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
783 internal bool IsBinded
789 static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
791 bindable.inheritedContext = null;
792 bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
793 bindable.OnBindingContextChanged();
795 if (newvalue is BindableObject targetBindableObject)
797 targetBindableObject.IsBinded = true;
801 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
803 object context = bindable.inheritedContext;
804 var oldBinding = oldBindingBase as Binding;
805 var newBinding = newBindingBase as Binding;
807 if (context == null && oldBinding != null)
808 context = oldBinding.Context;
809 if (context != null && newBinding != null)
810 newBinding.Context = context;
813 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
815 if (property == null)
816 throw new ArgumentNullException(nameof(property));
818 if (checkAccess && property.IsReadOnly)
819 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
821 BindablePropertyContext bpcontext = GetContext(property);
822 if (bpcontext == null)
825 if (fromStyle && !CanBeSetFromStyle(property))
828 object original = bpcontext.Value;
830 object newValue = property.GetDefaultValue(this);
832 bool same = Equals(original, newValue);
835 property.PropertyChanging?.Invoke(this, original, newValue);
837 OnPropertyChanging(property.PropertyName);
840 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
841 bpcontext.Value = newValue;
842 if (property.DefaultValueCreator == null)
843 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
845 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
849 OnPropertyChanged(property.PropertyName);
850 OnPropertyChangedWithData(property);
851 property.PropertyChanged?.Invoke(this, original, newValue);
855 [MethodImpl(MethodImplOptions.AggressiveInlining)]
856 BindablePropertyContext CreateAndAddContext(BindableProperty property)
858 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
860 if (property.DefaultValueCreator == null)
861 context.Attributes = BindableContextAttributes.IsDefaultValue;
863 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
865 properties.Add(property, context);
869 [MethodImpl(MethodImplOptions.AggressiveInlining)]
870 BindablePropertyContext GetContext(BindableProperty property) => properties.TryGetValue(property, out var result) ? result : null;
872 [MethodImpl(MethodImplOptions.AggressiveInlining)]
873 BindablePropertyContext GetOrCreateContext(BindableProperty property)
875 BindablePropertyContext context = GetContext(property);
878 context = CreateAndAddContext(property);
880 else if (property.ValueGetter != null)
882 context.Value = property.ValueGetter(this); //Update Value from dali
883 }//added by xiaohui.fang
884 else if (property.DefaultValueCreator != null) //This will be removed in the future.
886 context.Value = property.DefaultValueCreator(this); //Update Value from dali
892 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
894 context.Binding.Unapply();
896 property.BindingChanging?.Invoke(this, context.Binding, null);
898 context.Binding = null;
901 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
903 if (property == null)
904 throw new ArgumentNullException(nameof(property));
906 if (checkAccess && property.IsReadOnly)
907 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
909 if (fromStyle && !CanBeSetFromStyle(property))
912 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
913 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
917 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
919 object original = context.Value;
920 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
921 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
922 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
923 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
925 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
926 if (!silent && (!same || raiseOnEqual))
928 property.PropertyChanging?.Invoke(this, original, value);
930 OnPropertyChanging(property.PropertyName);
933 if (!same || raiseOnEqual)
935 context.Value = value;
938 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
939 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
941 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
942 RemoveDynamicResource(property);
944 BindingBase binding = context.Binding;
947 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
949 RemoveBinding(property, context);
954 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
958 if ((!same || raiseOnEqual))
960 property.PropertyChanged?.Invoke(this, original, value);
962 if (binding != null && !currentlyApplying)
969 OnPropertyChanged(property.PropertyName);
971 if (null != propertyGroup)
973 foreach (var relativeProperty in propertyGroup)
975 if (relativeProperty != property)
977 var relativeContext = GetOrCreateContext(relativeProperty);
978 var relativeBinding = relativeContext.Binding;
980 if (null != relativeBinding)
983 relativeBinding.Apply(true);
987 OnPropertyChanged(relativeProperty.PropertyName);
992 else if (true == same && true == forceSendChangeSignal)
994 if (binding != null && !currentlyApplying)
1001 OnPropertyChanged(property.PropertyName);
1003 if (null != propertyGroup)
1005 foreach (var relativeProperty in propertyGroup)
1007 if (relativeProperty != property)
1009 var relativeContext = GetOrCreateContext(relativeProperty);
1010 var relativeBinding = relativeContext.Binding;
1012 if (null != relativeBinding)
1015 relativeBinding.Apply(true);
1019 OnPropertyChanged(relativeProperty.PropertyName);
1025 OnPropertyChangedWithData(property);
1029 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
1030 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1033 enum BindableContextAttributes
1035 IsManuallySet = 1 << 0,
1036 IsBeingSet = 1 << 1,
1037 IsDynamicResource = 1 << 2,
1038 IsSetFromStyle = 1 << 3,
1039 IsDefaultValue = 1 << 4,
1040 IsDefaultValueCreated = 1 << 5,
1043 class BindablePropertyContext
1045 public BindableContextAttributes Attributes;
1046 public BindingBase Binding;
1047 public Queue<SetValueArgs> DelayedSetters;
1048 public BindableProperty Property;
1049 public object Value;
1053 internal enum SetValuePrivateFlags
1056 CheckAccess = 1 << 0,
1058 ManuallySet = 1 << 2,
1061 Default = CheckAccess
1066 public readonly SetValueFlags Attributes;
1067 public readonly BindablePropertyContext Context;
1068 public readonly bool CurrentlyApplying;
1069 public readonly BindableProperty Property;
1070 public readonly object Value;
1072 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1074 Property = property;
1077 CurrentlyApplying = currentlyApplying;
1078 Attributes = attributes;
1082 internal void ReplaceBindingElement(Dictionary<string, object> oldNameScope, Dictionary<string, object> newNameScope)
1084 var xElementToNameOfOld = new Dictionary<object, string>();
1086 foreach (var pair in oldNameScope)
1088 xElementToNameOfOld.Add(pair.Value, pair.Key);
1091 foreach (var property in properties)
1093 if (property.Value.Binding is Binding binding && null != binding.Source)
1096 xElementToNameOfOld.TryGetValue(binding.Source, out xName);
1100 var newObject = newNameScope[xName];
1102 binding.Source = newObject;
1103 SetBinding(property.Key, binding);
1108 if (null != BindingContext)
1111 xElementToNameOfOld.TryGetValue(BindingContext, out xName);
1115 var newObject = newNameScope[xName];
1116 BindingContext = newObject;
1121 internal void ClearBinding()
1123 foreach (var property in properties)
1125 if (null != property.Value.Binding)
1127 property.Value.Binding.Unapply();
1134 namespace Tizen.NUI.Binding.Internals
1137 /// SetValueFlags. For internal use.
1140 [EditorBrowsable(EditorBrowsableState.Never)]
1141 public enum SetValueFlags
1146 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1147 [EditorBrowsable(EditorBrowsableState.Never)]
1151 /// Clear OneWay bindings.
1153 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1154 [EditorBrowsable(EditorBrowsableState.Never)]
1155 ClearOneWayBindings = 1 << 0,
1158 /// Clear TwoWay bindings.
1160 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1161 [EditorBrowsable(EditorBrowsableState.Never)]
1162 ClearTwoWayBindings = 1 << 1,
1165 /// Clear dynamic resource.
1167 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1168 [EditorBrowsable(EditorBrowsableState.Never)]
1169 ClearDynamicResource = 1 << 2,
1174 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1175 [EditorBrowsable(EditorBrowsableState.Never)]
1176 RaiseOnEqual = 1 << 3