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();
48 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
50 if (null != bindable.bindingContext)
52 return bindable.bindingContext;
55 if (bindable is Container container)
57 return container.Parent?.BindingContext;
65 readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
68 object inheritedContext;
70 private object bindingContext;
73 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
75 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public object BindingContext
79 get { return inheritedContext ?? GetValue(BindingContextProperty); }
80 set { SetValue(BindingContextProperty, value); }
83 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
85 SetDynamicResource(property, key, false);
89 /// Raised when a property has changed.
91 /// <since_tizen> 9 </since_tizen>
92 public event PropertyChangedEventHandler PropertyChanged;
94 /// <summary>Copy properties of other ViewStyle to this.</summary>
95 /// <param name="other">The other BindableProperty merge to this.</param>
96 [EditorBrowsable(EditorBrowsableState.Never)]
97 public virtual void CopyFrom(BindableObject other)
99 if (null == other) return;
101 Type type1 = this.GetType();
102 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
104 Type type2 = other.GetType();
105 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
107 if (null != nameToBindableProperty1)
109 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
111 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
113 if (null != bindableProperty)
115 object value = other.GetValue(bindableProperty);
119 SetValue(keyValuePair.Value, value);
127 /// Raised whenever the BindingContext property changes.
129 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
130 [EditorBrowsable(EditorBrowsableState.Never)]
131 public event EventHandler BindingContextChanged;
133 internal void ClearValue(BindableProperty property, bool fromStyle)
135 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
139 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
141 /// <param name="property">The BindableProperty to clear</param>
142 internal void ClearValue(BindableProperty property)
144 ClearValue(property, fromStyle: false, checkAccess: true);
148 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
150 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
151 internal void ClearValue(BindablePropertyKey propertyKey)
153 if (propertyKey == null)
154 throw new ArgumentNullException(nameof(propertyKey));
156 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
160 /// Return true if the target property exists and has been set.
162 /// <param name="targetProperty">The target property</param>
163 /// <returns>return true if the target property exists and has been set</returns>
164 internal bool IsSet(BindableProperty targetProperty)
166 if (targetProperty == null)
167 throw new ArgumentNullException(nameof(targetProperty));
169 var bpcontext = GetContext(targetProperty);
170 return bpcontext != null
171 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
175 /// Returns the value that is contained the BindableProperty.
177 /// <param name="property">The BindableProperty for which to get the value.</param>
178 /// <returns>The value that is contained the BindableProperty</returns>
179 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
180 [EditorBrowsable(EditorBrowsableState.Never)]
181 public object GetValue(BindableProperty property)
183 if (property == null)
184 throw new ArgumentNullException(nameof(property));
186 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
189 return property.DefaultValue;
191 return context.Value;
195 /// Raised when a property is about to change.
197 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 public event PropertyChangingEventHandler PropertyChanging;
202 /// Removes a previously set binding.
204 /// <param name="property">The BindableProperty from which to remove bindings.</param>
205 internal void RemoveBinding(BindableProperty property)
207 if (property == null)
208 throw new ArgumentNullException(nameof(property));
210 BindablePropertyContext context = GetContext(property);
211 if (context == null || context.Binding == null)
214 RemoveBinding(property, context);
218 /// Assigns a binding to a property.
220 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
221 /// <param name="binding">The binding to set.</param>
222 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
223 [EditorBrowsable(EditorBrowsableState.Never)]
224 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
226 SetBinding(targetProperty, binding, false);
229 private bool isCreateByXaml = false;
230 /// Only used by the IL of xaml, will never changed to not hidden.
231 [EditorBrowsable(EditorBrowsableState.Never)]
232 public virtual bool IsCreateByXaml
236 return isCreateByXaml;
240 isCreateByXaml = value;
245 /// Sets the value of the specified property.
247 /// <param name="property">The BindableProperty on which to assign a value.</param>
248 /// <param name="value">The value to set.</param>
249 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
250 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
251 [EditorBrowsable(EditorBrowsableState.Never)]
252 public void SetValue(BindableProperty property, object value)
254 if (true == isCreateByXaml)
256 SetValue(property, value, false, true);
260 if (null == property)
262 throw new ArgumentNullException(nameof(property));
264 property.PropertyChanged?.Invoke(this, null, value);
266 OnPropertyChanged(property.PropertyName);
267 OnPropertyChangedWithData(property);
271 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
273 if (property == null)
274 throw new ArgumentNullException(nameof(property));
276 if (true == isCreateByXaml)
278 if (property.IsReadOnly)
279 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
281 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
282 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
286 property.PropertyChanged?.Invoke(this, null, value);
291 /// Sets the value of the propertyKey.
293 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
294 /// <param name="value">The value to set.</param>
295 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
296 [EditorBrowsable(EditorBrowsableState.Never)]
297 public void SetValue(BindablePropertyKey propertyKey, object value)
299 if (propertyKey == null)
300 throw new ArgumentNullException(nameof(propertyKey));
302 SetValue(propertyKey.BindableProperty, value, false, false);
306 /// Set the inherited context to a neated element.
308 /// <param name="bindable">The object on which to set the inherited binding context.</param>
309 /// <param name="value">The inherited context to set.</param>
310 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
311 [EditorBrowsable(EditorBrowsableState.Never)]
312 public static void SetInheritedBindingContext(BindableObject bindable, object value)
314 if (null == bindable)
316 throw new ArgumentNullException(nameof(bindable));
319 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
320 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
323 object oldContext = bindable.inheritedContext;
325 if (ReferenceEquals(oldContext, value))
328 if (bpContext != null && oldContext == null)
329 oldContext = bpContext.Value;
331 if (bpContext != null && bpContext.Binding != null)
333 bpContext.Binding.Context = value;
334 bindable.inheritedContext = null;
338 bindable.inheritedContext = value;
341 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
342 bindable.OnBindingContextChanged();
346 /// Register the properties which can effect each other in Binding to same group.
348 [EditorBrowsable(EditorBrowsableState.Never)]
349 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
351 if (!PropertyToGroup.ContainsKey(property))
353 PropertyToGroup.Add(property, group);
357 /// Apply the bindings to BindingContext.
359 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
360 [EditorBrowsable(EditorBrowsableState.Never)]
361 protected void ApplyBindings()
363 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
367 /// Override this method to execute an action when the BindingContext changes.
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 virtual void OnBindingContextChanged()
373 BindingContextChanged?.Invoke(this, EventArgs.Empty);
377 /// Call this method from a child class to notify that a change happened on a property.
379 /// <param name="propertyName">The name of the property that changed.</param>
380 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
383 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
386 /// Call this method from a child class to notify that a change is going to happen on a property.
388 /// <param name="propertyName">The name of the property that is changing.</param>
389 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
390 [EditorBrowsable(EditorBrowsableState.Never)]
391 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
392 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
395 /// Method that is called when a bound property is changed.
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
401 /// Unapplies all previously set bindings.
403 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 protected void UnapplyBindings()
407 for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
409 BindablePropertyContext context = properties[i];
410 if (context.Binding == null)
413 context.Binding.Unapply();
417 internal bool GetIsBound(BindableProperty targetProperty)
419 if (targetProperty == null)
420 throw new ArgumentNullException(nameof(targetProperty));
422 BindablePropertyContext bpcontext = GetContext(targetProperty);
423 return bpcontext != null && bpcontext.Binding != null;
427 /// Returns the value that is contained the BindableProperty.
429 /// <param name="property0">The BindableProperty instance.</param>
430 /// <param name="property1">The BindableProperty instance.</param>
431 /// <returns>The value that is contained the BindableProperty</returns>
432 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
434 var values = new object[2];
436 for (var i = 0; i < properties.Count; i++)
438 BindablePropertyContext context = properties[i];
440 if (ReferenceEquals(context.Property, property0))
442 values[0] = context.Value;
445 else if (ReferenceEquals(context.Property, property1))
447 values[1] = context.Value;
451 if (property0 == null && property1 == null)
455 if (!ReferenceEquals(property0, null))
456 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
457 if (!ReferenceEquals(property1, null))
458 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
464 /// Returns the value that is contained the BindableProperty.
466 /// <param name="property0">The BindableProperty instance.</param>
467 /// <param name="property1">The BindableProperty instance.</param>
468 /// <param name="property2">The BindableProperty instance.</param>
469 /// <returns>The value that is contained the BindableProperty</returns>
470 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
472 var values = new object[3];
474 for (var i = 0; i < properties.Count; i++)
476 BindablePropertyContext context = properties[i];
478 if (ReferenceEquals(context.Property, property0))
480 values[0] = context.Value;
483 else if (ReferenceEquals(context.Property, property1))
485 values[1] = context.Value;
488 else if (ReferenceEquals(context.Property, property2))
490 values[2] = context.Value;
494 if (property0 == null && property1 == null && property2 == null)
498 if (!ReferenceEquals(property0, null))
499 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
500 if (!ReferenceEquals(property1, null))
501 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
502 if (!ReferenceEquals(property2, null))
503 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
509 /// Returns the value that is contained the BindableProperty.
511 /// <param name="properties">The array of the BindableProperty instances</param>
512 /// <returns>The values that is contained the BindableProperty instances.</returns>
513 internal object[] GetValues(params BindableProperty[] properties)
515 var values = new object[properties.Length];
516 for (var i = 0; i < this.properties.Count; i++)
518 var context = this.properties[i];
519 var index = properties.IndexOf(context.Property);
522 values[index] = context.Value;
524 for (var i = 0; i < values.Length; i++)
526 if (!ReferenceEquals(values[i], null))
528 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
533 internal virtual void OnRemoveDynamicResource(BindableProperty property)
537 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
541 internal void RemoveDynamicResource(BindableProperty property)
543 if (property == null)
544 throw new ArgumentNullException(nameof(property));
546 OnRemoveDynamicResource(property);
547 BindablePropertyContext context = GetOrCreateContext(property);
548 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
551 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
553 if (targetProperty == null)
554 throw new ArgumentNullException(nameof(targetProperty));
556 throw new ArgumentNullException(nameof(binding));
558 if (fromStyle && !CanBeSetFromStyle(targetProperty))
561 IsCreateByXaml = true;
563 var context = GetOrCreateContext(targetProperty);
565 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
567 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
569 if (context.Binding != null)
570 context.Binding.Unapply();
572 BindingBase oldBinding = context.Binding;
573 context.Binding = binding;
575 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
577 binding.Apply(BindingContext, this, targetProperty);
580 bool CanBeSetFromStyle(BindableProperty property)
582 var context = GetContext(property);
585 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
587 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
589 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
594 internal void SetDynamicResource(BindableProperty property, string key)
596 SetDynamicResource(property, key, false);
599 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
601 if (property == null)
602 throw new ArgumentNullException(nameof(property));
603 if (string.IsNullOrEmpty(key))
604 throw new ArgumentNullException(nameof(key));
605 if (fromStyle && !CanBeSetFromStyle(property))
608 var context = GetOrCreateContext(property);
610 context.Attributes |= BindableContextAttributes.IsDynamicResource;
612 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
614 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
616 OnSetDynamicResource(property, key);
619 internal void SetValue(BindableProperty property, object value, bool fromStyle)
621 SetValue(property, value, fromStyle, true);
624 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
626 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
630 /// For internal use.
632 /// <param name="property">The BindableProperty on which to assign a value.</param>
633 /// <param name="value">The value to set</param>
634 /// <param name="attributes">The set value flag</param>
635 [EditorBrowsable(EditorBrowsableState.Never)]
636 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
638 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
641 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
643 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
644 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
645 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
646 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
647 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
649 if (property == null)
650 throw new ArgumentNullException(nameof(property));
651 if (checkAccess && property.IsReadOnly)
653 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
657 if (!converted && !property.TryConvert(ref value))
659 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
663 if (property.ValidateValue != null && !property.ValidateValue(this, value))
664 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
666 if (property.CoerceValue != null)
667 value = property.CoerceValue(this, value);
669 BindablePropertyContext context = GetOrCreateContext(property);
672 context.Attributes |= BindableContextAttributes.IsManuallySet;
673 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
677 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
682 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
684 // else omitted on purpose
686 bool currentlyApplying = applying;
688 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
690 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
691 if (delayQueue == null)
692 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
694 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
698 context.Attributes |= BindableContextAttributes.IsBeingSet;
699 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
701 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
702 if (delayQueue != null)
704 while (delayQueue.Count > 0)
706 SetValueArgs s = delayQueue.Dequeue();
707 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
710 context.DelayedSetters = null;
713 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
717 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
719 var prop = properties.ToArray();
720 for (int i = 0, propLength = prop.Length; i < propLength; i++)
722 BindablePropertyContext context = prop[i];
723 BindingBase binding = context.Binding;
727 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
730 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
731 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
735 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
737 object context = bindable.inheritedContext;
738 var oldBinding = oldBindingBase as Binding;
739 var newBinding = newBindingBase as Binding;
741 if (context == null && oldBinding != null)
742 context = oldBinding.Context;
743 if (context != null && newBinding != null)
744 newBinding.Context = context;
747 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
749 if (property == null)
750 throw new ArgumentNullException(nameof(property));
752 if (checkAccess && property.IsReadOnly)
753 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
755 BindablePropertyContext bpcontext = GetContext(property);
756 if (bpcontext == null)
759 if (fromStyle && !CanBeSetFromStyle(property))
762 object original = bpcontext.Value;
764 object newValue = property.GetDefaultValue(this);
766 bool same = Equals(original, newValue);
769 property.PropertyChanging?.Invoke(this, original, newValue);
771 OnPropertyChanging(property.PropertyName);
774 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
775 bpcontext.Value = newValue;
776 if (property.DefaultValueCreator == null)
777 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
779 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
783 OnPropertyChanged(property.PropertyName);
784 OnPropertyChangedWithData(property);
785 property.PropertyChanged?.Invoke(this, original, newValue);
789 [MethodImpl(MethodImplOptions.AggressiveInlining)]
790 BindablePropertyContext CreateAndAddContext(BindableProperty property)
792 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
794 if (property.DefaultValueCreator == null)
795 context.Attributes = BindableContextAttributes.IsDefaultValue;
797 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
799 properties.Add(context);
803 [MethodImpl(MethodImplOptions.AggressiveInlining)]
804 BindablePropertyContext GetContext(BindableProperty property)
806 List<BindablePropertyContext> propertyList = properties;
808 for (var i = 0; i < propertyList.Count; i++)
810 BindablePropertyContext context = propertyList[i];
811 if (ReferenceEquals(context.Property, property))
818 [MethodImpl(MethodImplOptions.AggressiveInlining)]
819 BindablePropertyContext GetOrCreateContext(BindableProperty property)
821 BindablePropertyContext context = GetContext(property);
824 context = CreateAndAddContext(property);
826 else if (property.DefaultValueCreator != null)
828 context.Value = property.DefaultValueCreator(this); //Update Value from dali
834 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
836 context.Binding.Unapply();
838 property.BindingChanging?.Invoke(this, context.Binding, null);
840 context.Binding = null;
843 void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
845 if (property == null)
846 throw new ArgumentNullException(nameof(property));
848 if (checkAccess && property.IsReadOnly)
849 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
851 if (fromStyle && !CanBeSetFromStyle(property))
854 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
855 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
859 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
861 object original = context.Value;
862 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
863 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
864 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
865 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
867 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
868 if (!silent && (!same || raiseOnEqual))
870 property.PropertyChanging?.Invoke(this, original, value);
872 OnPropertyChanging(property.PropertyName);
875 if (!same || raiseOnEqual)
877 context.Value = value;
880 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
881 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
883 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
884 RemoveDynamicResource(property);
886 BindingBase binding = context.Binding;
889 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
891 RemoveBinding(property, context);
896 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
900 if ((!same || raiseOnEqual))
902 property.PropertyChanged?.Invoke(this, original, value);
904 if (binding != null && !currentlyApplying)
911 OnPropertyChanged(property.PropertyName);
913 if (null != propertyGroup)
915 foreach (var relativeProperty in propertyGroup)
917 if (relativeProperty != property)
919 var relativeContext = GetOrCreateContext(relativeProperty);
920 var relativeBinding = relativeContext.Binding;
922 if (null != relativeBinding)
925 relativeBinding.Apply(true);
929 OnPropertyChanged(relativeProperty.PropertyName);
934 else if (true == same && true == forceSendChangeSignal)
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);
967 OnPropertyChangedWithData(property);
971 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
972 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
975 enum BindableContextAttributes
977 IsManuallySet = 1 << 0,
979 IsDynamicResource = 1 << 2,
980 IsSetFromStyle = 1 << 3,
981 IsDefaultValue = 1 << 4,
982 IsDefaultValueCreated = 1 << 5,
985 class BindablePropertyContext
987 public BindableContextAttributes Attributes;
988 public BindingBase Binding;
989 public Queue<SetValueArgs> DelayedSetters;
990 public BindableProperty Property;
995 internal enum SetValuePrivateFlags
998 CheckAccess = 1 << 0,
1000 ManuallySet = 1 << 2,
1003 Default = CheckAccess
1008 public readonly SetValueFlags Attributes;
1009 public readonly BindablePropertyContext Context;
1010 public readonly bool CurrentlyApplying;
1011 public readonly BindableProperty Property;
1012 public readonly object Value;
1014 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1016 Property = property;
1019 CurrentlyApplying = currentlyApplying;
1020 Attributes = attributes;
1024 internal void AddChildBindableObject(BindableObject child)
1028 children.Add(child);
1029 child.FlushBinding();
1033 internal void RemoveChildBindableObject(BindableObject child)
1035 children.Remove(child);
1038 private List<BindableObject> children = new List<BindableObject>();
1040 private void FlushBinding()
1042 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1043 OnBindingContextChanged();
1045 foreach (var child in children)
1047 child.FlushBinding();
1053 namespace Tizen.NUI.Binding.Internals
1056 /// SetValueFlags. For internal use.
1059 [EditorBrowsable(EditorBrowsableState.Never)]
1060 public enum SetValueFlags
1065 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1066 [EditorBrowsable(EditorBrowsableState.Never)]
1070 /// Clear OneWay bindings.
1072 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1073 [EditorBrowsable(EditorBrowsableState.Never)]
1074 ClearOneWayBindings = 1 << 0,
1077 /// Clear TwoWay bindings.
1079 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1080 [EditorBrowsable(EditorBrowsableState.Never)]
1081 ClearTwoWayBindings = 1 << 1,
1084 /// Clear dynamic resource.
1086 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1087 [EditorBrowsable(EditorBrowsableState.Never)]
1088 ClearDynamicResource = 1 << 2,
1093 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1094 [EditorBrowsable(EditorBrowsableState.Never)]
1095 RaiseOnEqual = 1 << 3