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.IsBinded = 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 == IsBinded)
261 SetValue(property, value, false, true);
265 if (null == property)
267 throw new ArgumentNullException(nameof(property));
270 object oldvalue = null;
271 if (null == property.DefaultValueCreator)
273 BindablePropertyContext context = GetOrCreateContext(property);
276 oldvalue = context.Value;
277 context.Value = value;
282 oldvalue = property.DefaultValueCreator.Invoke(this);
285 property.PropertyChanged?.Invoke(this, oldvalue, value);
287 OnPropertyChanged(property.PropertyName);
288 OnPropertyChangedWithData(property);
292 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
294 if (property == null)
295 throw new ArgumentNullException(nameof(property));
297 if (true == isCreateByXaml)
299 if (property.IsReadOnly)
300 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
302 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
303 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
307 property.PropertyChanged?.Invoke(this, null, value);
312 /// Sets the value of the propertyKey.
314 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
315 /// <param name="value">The value to set.</param>
316 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
317 [EditorBrowsable(EditorBrowsableState.Never)]
318 public void SetValue(BindablePropertyKey propertyKey, object value)
320 if (propertyKey == null)
321 throw new ArgumentNullException(nameof(propertyKey));
323 SetValue(propertyKey.BindableProperty, value, false, false);
327 /// Set the inherited context to a neated element.
329 /// <param name="bindable">The object on which to set the inherited binding context.</param>
330 /// <param name="value">The inherited context to set.</param>
331 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
332 [EditorBrowsable(EditorBrowsableState.Never)]
333 public static void SetInheritedBindingContext(BindableObject bindable, object value)
335 if (null == bindable)
337 throw new ArgumentNullException(nameof(bindable));
340 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
341 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
344 object oldContext = bindable.inheritedContext;
346 if (ReferenceEquals(oldContext, value))
349 if (bpContext != null && oldContext == null)
350 oldContext = bpContext.Value;
352 if (bpContext != null && bpContext.Binding != null)
354 bpContext.Binding.Context = value;
355 bindable.inheritedContext = null;
359 bindable.inheritedContext = value;
362 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
363 bindable.OnBindingContextChanged();
367 /// Register the properties which can effect each other in Binding to same group.
369 [EditorBrowsable(EditorBrowsableState.Never)]
370 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
372 if (!PropertyToGroup.ContainsKey(property))
374 PropertyToGroup.Add(property, group);
377 if (null != group && !(group.Contains(property)))
383 /// Apply the bindings to BindingContext.
385 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
386 [EditorBrowsable(EditorBrowsableState.Never)]
387 protected void ApplyBindings()
389 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
393 /// Override this method to execute an action when the BindingContext changes.
395 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
396 [EditorBrowsable(EditorBrowsableState.Never)]
397 protected virtual void OnBindingContextChanged()
399 BindingContextChanged?.Invoke(this, EventArgs.Empty);
403 /// Call this method from a child class to notify that a change happened on a property.
405 /// <param name="propertyName">The name of the property that changed.</param>
406 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
407 [EditorBrowsable(EditorBrowsableState.Never)]
408 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
409 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
412 /// Call this method from a child class to notify that a change is going to happen on a property.
414 /// <param name="propertyName">The name of the property that is changing.</param>
415 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
416 [EditorBrowsable(EditorBrowsableState.Never)]
417 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
418 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
421 /// Method that is called when a bound property is changed.
423 [EditorBrowsable(EditorBrowsableState.Never)]
424 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
427 /// Unapplies all previously set bindings.
429 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
430 [EditorBrowsable(EditorBrowsableState.Never)]
431 protected void UnapplyBindings()
433 for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
435 BindablePropertyContext context = properties[i];
436 if (context.Binding == null)
439 context.Binding.Unapply();
443 internal bool GetIsBound(BindableProperty targetProperty)
445 if (targetProperty == null)
446 throw new ArgumentNullException(nameof(targetProperty));
448 BindablePropertyContext bpcontext = GetContext(targetProperty);
449 return bpcontext != null && bpcontext.Binding != null;
453 /// Returns the value that is contained the BindableProperty.
455 /// <param name="property0">The BindableProperty instance.</param>
456 /// <param name="property1">The BindableProperty instance.</param>
457 /// <returns>The value that is contained the BindableProperty</returns>
458 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
460 var values = new object[2];
462 for (var i = 0; i < properties.Count; i++)
464 BindablePropertyContext context = properties[i];
466 if (ReferenceEquals(context.Property, property0))
468 values[0] = context.Value;
471 else if (ReferenceEquals(context.Property, property1))
473 values[1] = context.Value;
477 if (property0 == null && property1 == null)
481 if (!ReferenceEquals(property0, null))
482 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
483 if (!ReferenceEquals(property1, null))
484 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
490 /// Returns the value that is contained the BindableProperty.
492 /// <param name="property0">The BindableProperty instance.</param>
493 /// <param name="property1">The BindableProperty instance.</param>
494 /// <param name="property2">The BindableProperty instance.</param>
495 /// <returns>The value that is contained the BindableProperty</returns>
496 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
498 var values = new object[3];
500 for (var i = 0; i < properties.Count; i++)
502 BindablePropertyContext context = properties[i];
504 if (ReferenceEquals(context.Property, property0))
506 values[0] = context.Value;
509 else if (ReferenceEquals(context.Property, property1))
511 values[1] = context.Value;
514 else if (ReferenceEquals(context.Property, property2))
516 values[2] = context.Value;
520 if (property0 == null && property1 == null && property2 == null)
524 if (!ReferenceEquals(property0, null))
525 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
526 if (!ReferenceEquals(property1, null))
527 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
528 if (!ReferenceEquals(property2, null))
529 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
535 /// Returns the value that is contained the BindableProperty.
537 /// <param name="properties">The array of the BindableProperty instances</param>
538 /// <returns>The values that is contained the BindableProperty instances.</returns>
539 internal object[] GetValues(params BindableProperty[] properties)
541 var values = new object[properties.Length];
542 for (var i = 0; i < this.properties.Count; i++)
544 var context = this.properties[i];
545 var index = properties.IndexOf(context.Property);
548 values[index] = context.Value;
550 for (var i = 0; i < values.Length; i++)
552 if (!ReferenceEquals(values[i], null))
554 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
559 internal virtual void OnRemoveDynamicResource(BindableProperty property)
563 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
567 internal void RemoveDynamicResource(BindableProperty property)
569 if (property == null)
570 throw new ArgumentNullException(nameof(property));
572 OnRemoveDynamicResource(property);
573 BindablePropertyContext context = GetOrCreateContext(property);
574 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
577 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
579 if (targetProperty == null)
580 throw new ArgumentNullException(nameof(targetProperty));
582 throw new ArgumentNullException(nameof(binding));
584 if (fromStyle && !CanBeSetFromStyle(targetProperty))
589 var context = GetOrCreateContext(targetProperty);
591 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
593 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
595 if (context.Binding != null)
596 context.Binding.Unapply();
598 BindingBase oldBinding = context.Binding;
599 context.Binding = binding;
601 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
603 binding.Apply(BindingContext, this, targetProperty);
606 bool CanBeSetFromStyle(BindableProperty property)
608 var context = GetContext(property);
611 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
613 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
615 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
620 internal void SetDynamicResource(BindableProperty property, string key)
622 SetDynamicResource(property, key, false);
625 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
627 if (property == null)
628 throw new ArgumentNullException(nameof(property));
629 if (string.IsNullOrEmpty(key))
630 throw new ArgumentNullException(nameof(key));
631 if (fromStyle && !CanBeSetFromStyle(property))
634 var context = GetOrCreateContext(property);
636 context.Attributes |= BindableContextAttributes.IsDynamicResource;
638 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
640 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
642 OnSetDynamicResource(property, key);
645 internal void SetValue(BindableProperty property, object value, bool fromStyle)
647 SetValue(property, value, fromStyle, true);
650 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
652 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
656 /// For internal use.
658 /// <param name="property">The BindableProperty on which to assign a value.</param>
659 /// <param name="value">The value to set</param>
660 /// <param name="attributes">The set value flag</param>
661 [EditorBrowsable(EditorBrowsableState.Never)]
662 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
664 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
667 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
669 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
670 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
671 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
672 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
673 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
675 if (property == null)
676 throw new ArgumentNullException(nameof(property));
677 if (checkAccess && property.IsReadOnly)
679 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
683 if (!converted && !property.TryConvert(ref value))
685 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
689 if (property.ValidateValue != null && !property.ValidateValue(this, value))
690 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
692 if (property.CoerceValue != null)
693 value = property.CoerceValue(this, value);
695 BindablePropertyContext context = GetOrCreateContext(property);
698 context.Attributes |= BindableContextAttributes.IsManuallySet;
699 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
703 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
708 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
710 // else omitted on purpose
712 bool currentlyApplying = applying;
714 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
716 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
717 if (delayQueue == null)
718 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
720 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
724 context.Attributes |= BindableContextAttributes.IsBeingSet;
725 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
727 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
728 if (delayQueue != null)
730 while (delayQueue.Count > 0)
732 SetValueArgs s = delayQueue.Dequeue();
733 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
736 context.DelayedSetters = null;
739 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
743 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
745 var prop = properties.ToArray();
746 for (int i = 0, propLength = prop.Length; i < propLength; i++)
748 BindablePropertyContext context = prop[i];
749 BindingBase binding = context.Binding;
753 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
756 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
757 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
761 internal bool IsBinded
767 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
769 object context = bindable.inheritedContext;
770 var oldBinding = oldBindingBase as Binding;
771 var newBinding = newBindingBase as Binding;
773 if (context == null && oldBinding != null)
774 context = oldBinding.Context;
775 if (context != null && newBinding != null)
776 newBinding.Context = context;
779 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
781 if (property == null)
782 throw new ArgumentNullException(nameof(property));
784 if (checkAccess && property.IsReadOnly)
785 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
787 BindablePropertyContext bpcontext = GetContext(property);
788 if (bpcontext == null)
791 if (fromStyle && !CanBeSetFromStyle(property))
794 object original = bpcontext.Value;
796 object newValue = property.GetDefaultValue(this);
798 bool same = Equals(original, newValue);
801 property.PropertyChanging?.Invoke(this, original, newValue);
803 OnPropertyChanging(property.PropertyName);
806 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
807 bpcontext.Value = newValue;
808 if (property.DefaultValueCreator == null)
809 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
811 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
815 OnPropertyChanged(property.PropertyName);
816 OnPropertyChangedWithData(property);
817 property.PropertyChanged?.Invoke(this, original, newValue);
821 [MethodImpl(MethodImplOptions.AggressiveInlining)]
822 BindablePropertyContext CreateAndAddContext(BindableProperty property)
824 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
826 if (property.DefaultValueCreator == null)
827 context.Attributes = BindableContextAttributes.IsDefaultValue;
829 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
831 properties.Add(context);
835 [MethodImpl(MethodImplOptions.AggressiveInlining)]
836 BindablePropertyContext GetContext(BindableProperty property)
838 List<BindablePropertyContext> propertyList = properties;
840 for (var i = 0; i < propertyList.Count; i++)
842 BindablePropertyContext context = propertyList[i];
843 if (ReferenceEquals(context.Property, property))
850 [MethodImpl(MethodImplOptions.AggressiveInlining)]
851 BindablePropertyContext GetOrCreateContext(BindableProperty property)
853 BindablePropertyContext context = GetContext(property);
856 context = CreateAndAddContext(property);
858 else if (property.DefaultValueCreator != null)
860 context.Value = property.DefaultValueCreator(this); //Update Value from dali
866 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
868 context.Binding.Unapply();
870 property.BindingChanging?.Invoke(this, context.Binding, null);
872 context.Binding = null;
875 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
877 if (property == null)
878 throw new ArgumentNullException(nameof(property));
880 if (checkAccess && property.IsReadOnly)
881 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
883 if (fromStyle && !CanBeSetFromStyle(property))
886 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
887 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
891 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
893 object original = context.Value;
894 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
895 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
896 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
897 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
899 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
900 if (!silent && (!same || raiseOnEqual))
902 property.PropertyChanging?.Invoke(this, original, value);
904 OnPropertyChanging(property.PropertyName);
907 if (!same || raiseOnEqual)
909 context.Value = value;
912 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
913 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
915 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
916 RemoveDynamicResource(property);
918 BindingBase binding = context.Binding;
921 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
923 RemoveBinding(property, context);
928 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
932 if ((!same || raiseOnEqual))
934 property.PropertyChanged?.Invoke(this, original, value);
936 if (binding != null && !currentlyApplying)
943 OnPropertyChanged(property.PropertyName);
945 if (null != propertyGroup)
947 foreach (var relativeProperty in propertyGroup)
949 if (relativeProperty != property)
951 var relativeContext = GetOrCreateContext(relativeProperty);
952 var relativeBinding = relativeContext.Binding;
954 if (null != relativeBinding)
957 relativeBinding.Apply(true);
961 OnPropertyChanged(relativeProperty.PropertyName);
966 else if (true == same && true == forceSendChangeSignal)
968 if (binding != null && !currentlyApplying)
975 OnPropertyChanged(property.PropertyName);
977 if (null != propertyGroup)
979 foreach (var relativeProperty in propertyGroup)
981 if (relativeProperty != property)
983 var relativeContext = GetOrCreateContext(relativeProperty);
984 var relativeBinding = relativeContext.Binding;
986 if (null != relativeBinding)
989 relativeBinding.Apply(true);
993 OnPropertyChanged(relativeProperty.PropertyName);
999 OnPropertyChangedWithData(property);
1003 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
1004 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1007 enum BindableContextAttributes
1009 IsManuallySet = 1 << 0,
1010 IsBeingSet = 1 << 1,
1011 IsDynamicResource = 1 << 2,
1012 IsSetFromStyle = 1 << 3,
1013 IsDefaultValue = 1 << 4,
1014 IsDefaultValueCreated = 1 << 5,
1017 class BindablePropertyContext
1019 public BindableContextAttributes Attributes;
1020 public BindingBase Binding;
1021 public Queue<SetValueArgs> DelayedSetters;
1022 public BindableProperty Property;
1023 public object Value;
1027 internal enum SetValuePrivateFlags
1030 CheckAccess = 1 << 0,
1032 ManuallySet = 1 << 2,
1035 Default = CheckAccess
1040 public readonly SetValueFlags Attributes;
1041 public readonly BindablePropertyContext Context;
1042 public readonly bool CurrentlyApplying;
1043 public readonly BindableProperty Property;
1044 public readonly object Value;
1046 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1048 Property = property;
1051 CurrentlyApplying = currentlyApplying;
1052 Attributes = attributes;
1056 internal void AddChildBindableObject(BindableObject child)
1060 children.Add(child);
1061 child.FlushBinding();
1065 internal void RemoveChildBindableObject(BindableObject child)
1067 children.Remove(child);
1070 private List<BindableObject> children = new List<BindableObject>();
1072 private void FlushBinding()
1074 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1075 OnBindingContextChanged();
1077 foreach (var child in children)
1079 child.FlushBinding();
1085 namespace Tizen.NUI.Binding.Internals
1088 /// SetValueFlags. For internal use.
1091 [EditorBrowsable(EditorBrowsableState.Never)]
1092 public enum SetValueFlags
1097 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1098 [EditorBrowsable(EditorBrowsableState.Never)]
1102 /// Clear OneWay bindings.
1104 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1105 [EditorBrowsable(EditorBrowsableState.Never)]
1106 ClearOneWayBindings = 1 << 0,
1109 /// Clear TwoWay bindings.
1111 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1112 [EditorBrowsable(EditorBrowsableState.Never)]
1113 ClearTwoWayBindings = 1 << 1,
1116 /// Clear dynamic resource.
1118 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1119 [EditorBrowsable(EditorBrowsableState.Never)]
1120 ClearDynamicResource = 1 << 2,
1125 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1126 [EditorBrowsable(EditorBrowsableState.Never)]
1127 RaiseOnEqual = 1 << 3