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 /// Apply the bindings to BindingContext.
348 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
349 [EditorBrowsable(EditorBrowsableState.Never)]
350 protected void ApplyBindings()
352 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
356 /// Override this method to execute an action when the BindingContext changes.
358 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
359 [EditorBrowsable(EditorBrowsableState.Never)]
360 protected virtual void OnBindingContextChanged()
362 BindingContextChanged?.Invoke(this, EventArgs.Empty);
366 /// Call this method from a child class to notify that a change happened on a property.
368 /// <param name="propertyName">The name of the property that changed.</param>
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 OnPropertyChanged([CallerMemberName] string propertyName = null)
372 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
375 /// Call this method from a child class to notify that a change is going to happen on a property.
377 /// <param name="propertyName">The name of the property that is changing.</param>
378 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
379 [EditorBrowsable(EditorBrowsableState.Never)]
380 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
381 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
384 /// Method that is called when a bound property is changed.
386 [EditorBrowsable(EditorBrowsableState.Never)]
387 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
390 /// Unapplies all previously set bindings.
392 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
393 [EditorBrowsable(EditorBrowsableState.Never)]
394 protected void UnapplyBindings()
396 for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
398 BindablePropertyContext context = properties[i];
399 if (context.Binding == null)
402 context.Binding.Unapply();
406 internal bool GetIsBound(BindableProperty targetProperty)
408 if (targetProperty == null)
409 throw new ArgumentNullException(nameof(targetProperty));
411 BindablePropertyContext bpcontext = GetContext(targetProperty);
412 return bpcontext != null && bpcontext.Binding != null;
416 /// Returns the value that is contained the BindableProperty.
418 /// <param name="property0">The BindableProperty instance.</param>
419 /// <param name="property1">The BindableProperty instance.</param>
420 /// <returns>The value that is contained the BindableProperty</returns>
421 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
423 var values = new object[2];
425 for (var i = 0; i < properties.Count; i++)
427 BindablePropertyContext context = properties[i];
429 if (ReferenceEquals(context.Property, property0))
431 values[0] = context.Value;
434 else if (ReferenceEquals(context.Property, property1))
436 values[1] = context.Value;
440 if (property0 == null && property1 == null)
444 if (!ReferenceEquals(property0, null))
445 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
446 if (!ReferenceEquals(property1, null))
447 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
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 /// <param name="property2">The BindableProperty instance.</param>
458 /// <returns>The value that is contained the BindableProperty</returns>
459 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
461 var values = new object[3];
463 for (var i = 0; i < properties.Count; i++)
465 BindablePropertyContext context = properties[i];
467 if (ReferenceEquals(context.Property, property0))
469 values[0] = context.Value;
472 else if (ReferenceEquals(context.Property, property1))
474 values[1] = context.Value;
477 else if (ReferenceEquals(context.Property, property2))
479 values[2] = context.Value;
483 if (property0 == null && property1 == null && property2 == null)
487 if (!ReferenceEquals(property0, null))
488 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
489 if (!ReferenceEquals(property1, null))
490 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
491 if (!ReferenceEquals(property2, null))
492 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
498 /// Returns the value that is contained the BindableProperty.
500 /// <param name="properties">The array of the BindableProperty instances</param>
501 /// <returns>The values that is contained the BindableProperty instances.</returns>
502 internal object[] GetValues(params BindableProperty[] properties)
504 var values = new object[properties.Length];
505 for (var i = 0; i < this.properties.Count; i++)
507 var context = this.properties[i];
508 var index = properties.IndexOf(context.Property);
511 values[index] = context.Value;
513 for (var i = 0; i < values.Length; i++)
515 if (!ReferenceEquals(values[i], null))
517 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
522 internal virtual void OnRemoveDynamicResource(BindableProperty property)
526 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
530 internal void RemoveDynamicResource(BindableProperty property)
532 if (property == null)
533 throw new ArgumentNullException(nameof(property));
535 OnRemoveDynamicResource(property);
536 BindablePropertyContext context = GetOrCreateContext(property);
537 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
540 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
542 if (targetProperty == null)
543 throw new ArgumentNullException(nameof(targetProperty));
545 throw new ArgumentNullException(nameof(binding));
547 if (fromStyle && !CanBeSetFromStyle(targetProperty))
550 IsCreateByXaml = true;
552 var context = GetOrCreateContext(targetProperty);
554 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
556 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
558 if (context.Binding != null)
559 context.Binding.Unapply();
561 BindingBase oldBinding = context.Binding;
562 context.Binding = binding;
564 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
566 binding.Apply(BindingContext, this, targetProperty);
569 bool CanBeSetFromStyle(BindableProperty property)
571 var context = GetContext(property);
574 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
576 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
578 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
583 internal void SetDynamicResource(BindableProperty property, string key)
585 SetDynamicResource(property, key, false);
588 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
590 if (property == null)
591 throw new ArgumentNullException(nameof(property));
592 if (string.IsNullOrEmpty(key))
593 throw new ArgumentNullException(nameof(key));
594 if (fromStyle && !CanBeSetFromStyle(property))
597 var context = GetOrCreateContext(property);
599 context.Attributes |= BindableContextAttributes.IsDynamicResource;
601 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
603 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
605 OnSetDynamicResource(property, key);
608 internal void SetValue(BindableProperty property, object value, bool fromStyle)
610 SetValue(property, value, fromStyle, true);
613 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
615 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
619 /// For internal use.
621 /// <param name="property">The BindableProperty on which to assign a value.</param>
622 /// <param name="value">The value to set</param>
623 /// <param name="attributes">The set value flag</param>
624 [EditorBrowsable(EditorBrowsableState.Never)]
625 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
627 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
630 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
632 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
633 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
634 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
635 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
636 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
638 if (property == null)
639 throw new ArgumentNullException(nameof(property));
640 if (checkAccess && property.IsReadOnly)
642 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
646 if (!converted && !property.TryConvert(ref value))
648 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
652 if (property.ValidateValue != null && !property.ValidateValue(this, value))
653 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
655 if (property.CoerceValue != null)
656 value = property.CoerceValue(this, value);
658 BindablePropertyContext context = GetOrCreateContext(property);
661 context.Attributes |= BindableContextAttributes.IsManuallySet;
662 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
665 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
668 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
669 // else omitted on purpose
671 bool currentlyApplying = applying;
673 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
675 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
676 if (delayQueue == null)
677 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
679 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
683 context.Attributes |= BindableContextAttributes.IsBeingSet;
684 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
686 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
687 if (delayQueue != null)
689 while (delayQueue.Count > 0)
691 SetValueArgs s = delayQueue.Dequeue();
692 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
695 context.DelayedSetters = null;
698 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
702 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
704 var prop = properties.ToArray();
705 for (int i = 0, propLength = prop.Length; i < propLength; i++)
707 BindablePropertyContext context = prop[i];
708 BindingBase binding = context.Binding;
712 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
715 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
716 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
720 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
722 object context = bindable.inheritedContext;
723 var oldBinding = oldBindingBase as Binding;
724 var newBinding = newBindingBase as Binding;
726 if (context == null && oldBinding != null)
727 context = oldBinding.Context;
728 if (context != null && newBinding != null)
729 newBinding.Context = context;
732 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
734 if (property == null)
735 throw new ArgumentNullException(nameof(property));
737 if (checkAccess && property.IsReadOnly)
738 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
740 BindablePropertyContext bpcontext = GetContext(property);
741 if (bpcontext == null)
744 if (fromStyle && !CanBeSetFromStyle(property))
747 object original = bpcontext.Value;
749 object newValue = property.GetDefaultValue(this);
751 bool same = Equals(original, newValue);
754 property.PropertyChanging?.Invoke(this, original, newValue);
756 OnPropertyChanging(property.PropertyName);
759 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
760 bpcontext.Value = newValue;
761 if (property.DefaultValueCreator == null)
762 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
764 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
768 OnPropertyChanged(property.PropertyName);
769 OnPropertyChangedWithData(property);
770 property.PropertyChanged?.Invoke(this, original, newValue);
774 [MethodImpl(MethodImplOptions.AggressiveInlining)]
775 BindablePropertyContext CreateAndAddContext(BindableProperty property)
777 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
779 if (property.DefaultValueCreator == null)
780 context.Attributes = BindableContextAttributes.IsDefaultValue;
782 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
784 properties.Add(context);
788 [MethodImpl(MethodImplOptions.AggressiveInlining)]
789 BindablePropertyContext GetContext(BindableProperty property)
791 List<BindablePropertyContext> propertyList = properties;
793 for (var i = 0; i < propertyList.Count; i++)
795 BindablePropertyContext context = propertyList[i];
796 if (ReferenceEquals(context.Property, property))
803 [MethodImpl(MethodImplOptions.AggressiveInlining)]
804 BindablePropertyContext GetOrCreateContext(BindableProperty property)
806 BindablePropertyContext context = GetContext(property);
809 context = CreateAndAddContext(property);
811 else if (property.DefaultValueCreator != null)
813 context.Value = property.DefaultValueCreator(this); //Update Value from dali
819 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
821 context.Binding.Unapply();
823 property.BindingChanging?.Invoke(this, context.Binding, null);
825 context.Binding = null;
828 void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
830 if (property == null)
831 throw new ArgumentNullException(nameof(property));
833 if (checkAccess && property.IsReadOnly)
834 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
836 if (fromStyle && !CanBeSetFromStyle(property))
839 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
840 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
844 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
846 object original = context.Value;
847 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
848 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
849 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
850 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
852 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
853 if (!silent && (!same || raiseOnEqual))
855 property.PropertyChanging?.Invoke(this, original, value);
857 OnPropertyChanging(property.PropertyName);
860 if (!same || raiseOnEqual)
862 context.Value = value;
865 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
866 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
868 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
869 RemoveDynamicResource(property);
871 BindingBase binding = context.Binding;
874 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
876 RemoveBinding(property, context);
883 if ((!same || raiseOnEqual))
885 property.PropertyChanged?.Invoke(this, original, value);
887 if (binding != null && !currentlyApplying)
894 OnPropertyChanged(property.PropertyName);
896 else if (true == same && true == forceSendChangeSignal)
898 if (binding != null && !currentlyApplying)
905 OnPropertyChanged(property.PropertyName);
908 OnPropertyChangedWithData(property);
913 enum BindableContextAttributes
915 IsManuallySet = 1 << 0,
917 IsDynamicResource = 1 << 2,
918 IsSetFromStyle = 1 << 3,
919 IsDefaultValue = 1 << 4,
920 IsDefaultValueCreated = 1 << 5,
923 class BindablePropertyContext
925 public BindableContextAttributes Attributes;
926 public BindingBase Binding;
927 public Queue<SetValueArgs> DelayedSetters;
928 public BindableProperty Property;
933 internal enum SetValuePrivateFlags
936 CheckAccess = 1 << 0,
938 ManuallySet = 1 << 2,
941 Default = CheckAccess
946 public readonly SetValueFlags Attributes;
947 public readonly BindablePropertyContext Context;
948 public readonly bool CurrentlyApplying;
949 public readonly BindableProperty Property;
950 public readonly object Value;
952 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
957 CurrentlyApplying = currentlyApplying;
958 Attributes = attributes;
962 internal void AddChildBindableObject(BindableObject child)
967 child.FlushBinding();
971 internal void RemoveChildBindableObject(BindableObject child)
973 children.Remove(child);
976 private List<BindableObject> children = new List<BindableObject>();
978 private void FlushBinding()
980 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
981 OnBindingContextChanged();
983 foreach (var child in children)
985 child.FlushBinding();
991 namespace Tizen.NUI.Binding.Internals
994 /// SetValueFlags. For internal use.
997 [EditorBrowsable(EditorBrowsableState.Never)]
998 public enum SetValueFlags
1003 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1004 [EditorBrowsable(EditorBrowsableState.Never)]
1008 /// Clear OneWay bindings.
1010 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1011 [EditorBrowsable(EditorBrowsableState.Never)]
1012 ClearOneWayBindings = 1 << 0,
1015 /// Clear TwoWay bindings.
1017 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1018 [EditorBrowsable(EditorBrowsableState.Never)]
1019 ClearTwoWayBindings = 1 << 1,
1022 /// Clear dynamic resource.
1024 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1025 [EditorBrowsable(EditorBrowsableState.Never)]
1026 ClearDynamicResource = 1 << 2,
1031 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1032 [EditorBrowsable(EditorBrowsableState.Never)]
1033 RaiseOnEqual = 1 << 3