2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Reflection;
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), default(object), BindingMode.OneWay, null, BindingContextPropertyChanged,
40 null, null, BindingContextPropertyBindingChanging);
42 readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
45 object inheritedContext;
47 private object bindingContext;
50 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
52 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public object BindingContext
56 get { return inheritedContext ?? GetValue(BindingContextProperty); }
57 set { SetValue(BindingContextProperty, value); }
60 [EditorBrowsable(EditorBrowsableState.Never)]
61 public int LineNumber { get; set; } = -1;
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public int LinePosition { get; set; } = -1;
66 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
68 SetDynamicResource(property, key, false);
72 /// Raised when a property has changed.
74 /// <since_tizen> 9 </since_tizen>
75 public event PropertyChangedEventHandler PropertyChanged;
77 /// <summary>Copy properties of other ViewStyle to this.</summary>
78 /// <param name="other">The other BindableProperty merge to this.</param>
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public virtual void CopyFrom(BindableObject other)
82 if (null == other) return;
84 Type type1 = this.GetType();
85 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
87 Type type2 = other.GetType();
88 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
90 if (null != nameToBindableProperty1)
92 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
94 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
96 if (null != bindableProperty)
98 object value = other.GetValue(bindableProperty);
102 SetValue(keyValuePair.Value, value);
110 /// Raised whenever the BindingContext property changes.
112 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
113 [EditorBrowsable(EditorBrowsableState.Never)]
114 public event EventHandler BindingContextChanged;
116 internal void ClearValue(BindableProperty property, bool fromStyle)
118 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
122 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
124 /// <param name="property">The BindableProperty to clear</param>
125 internal void ClearValue(BindableProperty property)
127 ClearValue(property, fromStyle: false, checkAccess: true);
131 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
133 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
134 internal void ClearValue(BindablePropertyKey propertyKey)
136 if (propertyKey == null)
137 throw new ArgumentNullException(nameof(propertyKey));
139 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
143 /// Return true if the target property exists and has been set.
145 /// <param name="targetProperty">The target property</param>
146 /// <returns>return true if the target property exists and has been set</returns>
147 internal bool IsSet(BindableProperty targetProperty)
149 if (targetProperty == null)
150 throw new ArgumentNullException(nameof(targetProperty));
152 var bpcontext = GetContext(targetProperty);
153 return bpcontext != null
154 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
158 /// Returns the value that is contained the BindableProperty.
160 /// <param name="property">The BindableProperty for which to get the value.</param>
161 /// <returns>The value that is contained the BindableProperty</returns>
162 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public object GetValue(BindableProperty property)
166 if (property == null)
167 throw new ArgumentNullException(nameof(property));
169 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
172 return property.DefaultValue;
174 return context.Value;
178 /// Raised when a property is about to change.
180 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 public event PropertyChangingEventHandler PropertyChanging;
185 /// Removes a previously set binding.
187 /// <param name="property">The BindableProperty from which to remove bindings.</param>
188 internal void RemoveBinding(BindableProperty property)
190 if (property == null)
191 throw new ArgumentNullException(nameof(property));
193 BindablePropertyContext context = GetContext(property);
194 if (context == null || context.Binding == null)
197 RemoveBinding(property, context);
201 /// Assigns a binding to a property.
203 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
204 /// <param name="binding">The binding to set.</param>
205 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
206 [EditorBrowsable(EditorBrowsableState.Never)]
207 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
209 SetBinding(targetProperty, binding, false);
212 /// Only used by the IL of xaml, will never changed to not hidden.
213 [EditorBrowsable(EditorBrowsableState.Never)]
214 public bool IsCreateByXaml
221 /// Sets the value of the specified property.
223 /// <param name="property">The BindableProperty on which to assign a value.</param>
224 /// <param name="value">The value to set.</param>
225 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
226 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 public void SetValue(BindableProperty property, object value)
230 if (true == IsBinded)
232 SetValue(property, value, false, true);
236 if (null == property)
238 throw new ArgumentNullException(nameof(property));
241 object oldvalue = null;
242 if (null == property.DefaultValueCreator)
244 BindablePropertyContext context = GetOrCreateContext(property);
247 oldvalue = context.Value;
248 context.Value = value;
253 oldvalue = property.DefaultValueCreator.Invoke(this);
256 property.PropertyChanged?.Invoke(this, oldvalue, value);
258 OnPropertyChanged(property.PropertyName);
259 OnPropertyChangedWithData(property);
263 /// This will be public opened in next ACR.
264 [EditorBrowsable(EditorBrowsableState.Never)]
265 public void ForceNotifyBindedInstance(BindableProperty property)
267 if (null != property)
269 BindablePropertyContext context = GetOrCreateContext(property);
271 if (null != context?.Binding)
274 context.Binding.Apply(true);
278 OnPropertyChanged(property.PropertyName);
280 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
282 if (null != propertyGroup)
284 foreach (var relativeProperty in propertyGroup)
286 if (relativeProperty != property)
288 var relativeContext = GetOrCreateContext(relativeProperty);
289 var relativeBinding = relativeContext.Binding;
291 if (null != relativeBinding)
294 relativeBinding.Apply(true);
298 OnPropertyChanged(relativeProperty.PropertyName);
306 /// Sets the value of the propertyKey.
308 /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
309 /// <param name="value">The value to set.</param>
310 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
311 [EditorBrowsable(EditorBrowsableState.Never)]
312 public void SetValue(BindablePropertyKey propertyKey, object value)
314 if (propertyKey == null)
315 throw new ArgumentNullException(nameof(propertyKey));
317 SetValue(propertyKey.BindableProperty, value, false, false);
321 /// Set the inherited context to a neated element.
323 /// <param name="bindable">The object on which to set the inherited binding context.</param>
324 /// <param name="value">The inherited context to set.</param>
325 /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
326 [EditorBrowsable(EditorBrowsableState.Never)]
327 public static void SetInheritedBindingContext(BindableObject bindable, object value)
329 if (null == bindable)
331 throw new ArgumentNullException(nameof(bindable));
334 BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
335 if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
338 object oldContext = bindable.inheritedContext;
340 if (ReferenceEquals(oldContext, value))
343 if (bpContext != null && oldContext == null)
344 oldContext = bpContext.Value;
346 if (bpContext != null && bpContext.Binding != null)
348 bpContext.Binding.Context = value;
349 bindable.inheritedContext = null;
353 bindable.inheritedContext = value;
356 bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
357 bindable.OnBindingContextChanged();
361 /// Register the properties which can effect each other in Binding to same group.
363 [EditorBrowsable(EditorBrowsableState.Never)]
364 public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
366 if (!PropertyToGroup.ContainsKey(property))
368 PropertyToGroup.Add(property, group);
371 if (null != group && !(group.Contains(property)))
377 /// Apply the bindings to BindingContext.
379 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
380 [EditorBrowsable(EditorBrowsableState.Never)]
381 protected void ApplyBindings()
383 ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
387 /// Override this method to execute an action when the BindingContext changes.
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 OnBindingContextChanged()
393 BindingContextChanged?.Invoke(this, EventArgs.Empty);
397 /// Call this method from a child class to notify that a change happened on a property.
399 /// <param name="propertyName">The name of the property that changed.</param>
400 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
401 [EditorBrowsable(EditorBrowsableState.Never)]
402 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
403 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
406 /// Call this method from a child class to notify that a change is going to happen on a property.
408 /// <param name="propertyName">The name of the property that is changing.</param>
409 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
410 [EditorBrowsable(EditorBrowsableState.Never)]
411 protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
412 => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
415 /// Method that is called when a bound property is changed.
417 [EditorBrowsable(EditorBrowsableState.Never)]
418 protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
421 /// Unapplies all previously set bindings.
423 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
424 [EditorBrowsable(EditorBrowsableState.Never)]
425 protected void UnapplyBindings()
427 for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
429 BindablePropertyContext context = properties[i];
430 if (context.Binding == null)
433 context.Binding.Unapply();
437 internal bool GetIsBound(BindableProperty targetProperty)
439 if (targetProperty == null)
440 throw new ArgumentNullException(nameof(targetProperty));
442 BindablePropertyContext bpcontext = GetContext(targetProperty);
443 return bpcontext != null && bpcontext.Binding != null;
447 /// Returns the value that is contained the BindableProperty.
449 /// <param name="property0">The BindableProperty instance.</param>
450 /// <param name="property1">The BindableProperty instance.</param>
451 /// <returns>The value that is contained the BindableProperty</returns>
452 internal object[] GetValues(BindableProperty property0, BindableProperty property1)
454 var values = new object[2];
456 for (var i = 0; i < properties.Count; i++)
458 BindablePropertyContext context = properties[i];
460 if (ReferenceEquals(context.Property, property0))
462 values[0] = context.Value;
465 else if (ReferenceEquals(context.Property, property1))
467 values[1] = context.Value;
471 if (property0 == null && property1 == null)
475 if (!ReferenceEquals(property0, null))
476 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
477 if (!ReferenceEquals(property1, null))
478 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
484 /// Returns the value that is contained the BindableProperty.
486 /// <param name="property0">The BindableProperty instance.</param>
487 /// <param name="property1">The BindableProperty instance.</param>
488 /// <param name="property2">The BindableProperty instance.</param>
489 /// <returns>The value that is contained the BindableProperty</returns>
490 internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
492 var values = new object[3];
494 for (var i = 0; i < properties.Count; i++)
496 BindablePropertyContext context = properties[i];
498 if (ReferenceEquals(context.Property, property0))
500 values[0] = context.Value;
503 else if (ReferenceEquals(context.Property, property1))
505 values[1] = context.Value;
508 else if (ReferenceEquals(context.Property, property2))
510 values[2] = context.Value;
514 if (property0 == null && property1 == null && property2 == null)
518 if (!ReferenceEquals(property0, null))
519 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
520 if (!ReferenceEquals(property1, null))
521 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
522 if (!ReferenceEquals(property2, null))
523 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
529 /// Returns the value that is contained the BindableProperty.
531 /// <param name="properties">The array of the BindableProperty instances</param>
532 /// <returns>The values that is contained the BindableProperty instances.</returns>
533 internal object[] GetValues(params BindableProperty[] properties)
535 var values = new object[properties.Length];
536 for (var i = 0; i < this.properties.Count; i++)
538 var context = this.properties[i];
539 var index = properties.IndexOf(context.Property);
542 values[index] = context.Value;
544 for (var i = 0; i < values.Length; i++)
546 if (!ReferenceEquals(values[i], null))
548 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
553 internal virtual void OnRemoveDynamicResource(BindableProperty property)
557 internal virtual void OnSetDynamicResource(BindableProperty property, string key)
561 internal void RemoveDynamicResource(BindableProperty property)
563 if (property == null)
564 throw new ArgumentNullException(nameof(property));
566 OnRemoveDynamicResource(property);
567 BindablePropertyContext context = GetOrCreateContext(property);
568 context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
571 internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
573 if (targetProperty == null)
574 throw new ArgumentNullException(nameof(targetProperty));
576 throw new ArgumentNullException(nameof(binding));
578 if (fromStyle && !CanBeSetFromStyle(targetProperty))
583 var context = GetOrCreateContext(targetProperty);
585 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
587 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
589 if (context.Binding != null)
590 context.Binding.Unapply();
592 BindingBase oldBinding = context.Binding;
593 context.Binding = binding;
595 targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
597 binding.Apply(BindingContext, this, targetProperty);
600 bool CanBeSetFromStyle(BindableProperty property)
602 var context = GetContext(property);
605 if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
607 if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
609 if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
614 internal void SetDynamicResource(BindableProperty property, string key)
616 SetDynamicResource(property, key, false);
619 internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
621 if (property == null)
622 throw new ArgumentNullException(nameof(property));
623 if (string.IsNullOrEmpty(key))
624 throw new ArgumentNullException(nameof(key));
625 if (fromStyle && !CanBeSetFromStyle(property))
628 var context = GetOrCreateContext(property);
630 context.Attributes |= BindableContextAttributes.IsDynamicResource;
632 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
634 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
636 OnSetDynamicResource(property, key);
639 internal void SetValue(BindableProperty property, object value, bool fromStyle)
641 SetValue(property, value, fromStyle, true);
644 internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
646 SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
650 /// For internal use.
652 /// <param name="property">The BindableProperty on which to assign a value.</param>
653 /// <param name="value">The value to set</param>
654 /// <param name="attributes">The set value flag</param>
655 [EditorBrowsable(EditorBrowsableState.Never)]
656 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
658 SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
661 internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
663 bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
664 bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
665 bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
666 bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
667 bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
669 if (property == null)
670 throw new ArgumentNullException(nameof(property));
671 if (checkAccess && property.IsReadOnly)
673 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
677 if (!converted && !property.TryConvert(ref value))
679 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
683 if (property.ValidateValue != null && !property.ValidateValue(this, value))
684 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
686 if (property.CoerceValue != null)
687 value = property.CoerceValue(this, value);
689 BindablePropertyContext context = GetOrCreateContext(property);
692 context.Attributes |= BindableContextAttributes.IsManuallySet;
693 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
697 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
702 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
704 // else omitted on purpose
706 bool currentlyApplying = applying;
708 if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
710 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
711 if (delayQueue == null)
712 context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
714 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
718 context.Attributes |= BindableContextAttributes.IsBeingSet;
719 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
721 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
722 if (delayQueue != null)
724 while (delayQueue.Count > 0)
726 SetValueArgs s = delayQueue.Dequeue();
727 SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
730 context.DelayedSetters = null;
733 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
737 internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
739 var prop = properties.ToArray();
740 for (int i = 0, propLength = prop.Length; i < propLength; i++)
742 BindablePropertyContext context = prop[i];
743 BindingBase binding = context.Binding;
747 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
750 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
751 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
755 internal virtual bool IsBinded
761 static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
763 bindable.inheritedContext = null;
764 bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
765 bindable.OnBindingContextChanged();
767 if (newvalue is BindableObject targetBindableObject)
769 targetBindableObject.IsBinded = true;
773 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
775 object context = bindable.inheritedContext;
776 var oldBinding = oldBindingBase as Binding;
777 var newBinding = newBindingBase as Binding;
779 if (context == null && oldBinding != null)
780 context = oldBinding.Context;
781 if (context != null && newBinding != null)
782 newBinding.Context = context;
785 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
787 if (property == null)
788 throw new ArgumentNullException(nameof(property));
790 if (checkAccess && property.IsReadOnly)
791 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
793 BindablePropertyContext bpcontext = GetContext(property);
794 if (bpcontext == null)
797 if (fromStyle && !CanBeSetFromStyle(property))
800 object original = bpcontext.Value;
802 object newValue = property.GetDefaultValue(this);
804 bool same = Equals(original, newValue);
807 property.PropertyChanging?.Invoke(this, original, newValue);
809 OnPropertyChanging(property.PropertyName);
812 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
813 bpcontext.Value = newValue;
814 if (property.DefaultValueCreator == null)
815 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
817 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
821 OnPropertyChanged(property.PropertyName);
822 OnPropertyChangedWithData(property);
823 property.PropertyChanged?.Invoke(this, original, newValue);
827 [MethodImpl(MethodImplOptions.AggressiveInlining)]
828 BindablePropertyContext CreateAndAddContext(BindableProperty property)
830 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
832 if (property.DefaultValueCreator == null)
833 context.Attributes = BindableContextAttributes.IsDefaultValue;
835 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
837 properties.Add(context);
841 [MethodImpl(MethodImplOptions.AggressiveInlining)]
842 BindablePropertyContext GetContext(BindableProperty property)
844 List<BindablePropertyContext> propertyList = properties;
846 for (var i = 0; i < propertyList.Count; i++)
848 BindablePropertyContext context = propertyList[i];
849 if (ReferenceEquals(context.Property, property))
856 [MethodImpl(MethodImplOptions.AggressiveInlining)]
857 BindablePropertyContext GetOrCreateContext(BindableProperty property)
859 BindablePropertyContext context = GetContext(property);
862 context = CreateAndAddContext(property);
864 else if (property.DefaultValueCreator != null)
866 context.Value = property.DefaultValueCreator(this); //Update Value from dali
872 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
874 context.Binding.Unapply();
876 property.BindingChanging?.Invoke(this, context.Binding, null);
878 context.Binding = null;
881 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
883 if (property == null)
884 throw new ArgumentNullException(nameof(property));
886 if (checkAccess && property.IsReadOnly)
887 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
889 if (fromStyle && !CanBeSetFromStyle(property))
892 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
893 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
897 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
899 object original = context.Value;
900 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
901 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
902 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
903 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
905 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
906 if (!silent && (!same || raiseOnEqual))
908 property.PropertyChanging?.Invoke(this, original, value);
910 OnPropertyChanging(property.PropertyName);
913 if (!same || raiseOnEqual)
915 context.Value = value;
918 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
919 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
921 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
922 RemoveDynamicResource(property);
924 BindingBase binding = context.Binding;
927 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
929 RemoveBinding(property, context);
934 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
938 if ((!same || raiseOnEqual))
940 property.PropertyChanged?.Invoke(this, original, value);
942 if (binding != null && !currentlyApplying)
949 OnPropertyChanged(property.PropertyName);
951 if (null != propertyGroup)
953 foreach (var relativeProperty in propertyGroup)
955 if (relativeProperty != property)
957 var relativeContext = GetOrCreateContext(relativeProperty);
958 var relativeBinding = relativeContext.Binding;
960 if (null != relativeBinding)
963 relativeBinding.Apply(true);
967 OnPropertyChanged(relativeProperty.PropertyName);
972 else if (true == same && true == forceSendChangeSignal)
974 if (binding != null && !currentlyApplying)
981 OnPropertyChanged(property.PropertyName);
983 if (null != propertyGroup)
985 foreach (var relativeProperty in propertyGroup)
987 if (relativeProperty != property)
989 var relativeContext = GetOrCreateContext(relativeProperty);
990 var relativeBinding = relativeContext.Binding;
992 if (null != relativeBinding)
995 relativeBinding.Apply(true);
999 OnPropertyChanged(relativeProperty.PropertyName);
1005 OnPropertyChangedWithData(property);
1009 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
1010 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1013 enum BindableContextAttributes
1015 IsManuallySet = 1 << 0,
1016 IsBeingSet = 1 << 1,
1017 IsDynamicResource = 1 << 2,
1018 IsSetFromStyle = 1 << 3,
1019 IsDefaultValue = 1 << 4,
1020 IsDefaultValueCreated = 1 << 5,
1023 class BindablePropertyContext
1025 public BindableContextAttributes Attributes;
1026 public BindingBase Binding;
1027 public Queue<SetValueArgs> DelayedSetters;
1028 public BindableProperty Property;
1029 public object Value;
1033 internal enum SetValuePrivateFlags
1036 CheckAccess = 1 << 0,
1038 ManuallySet = 1 << 2,
1041 Default = CheckAccess
1046 public readonly SetValueFlags Attributes;
1047 public readonly BindablePropertyContext Context;
1048 public readonly bool CurrentlyApplying;
1049 public readonly BindableProperty Property;
1050 public readonly object Value;
1052 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1054 Property = property;
1057 CurrentlyApplying = currentlyApplying;
1058 Attributes = attributes;
1064 namespace Tizen.NUI.Binding.Internals
1067 /// SetValueFlags. For internal use.
1070 [EditorBrowsable(EditorBrowsableState.Never)]
1071 public enum SetValueFlags
1076 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1077 [EditorBrowsable(EditorBrowsableState.Never)]
1081 /// Clear OneWay bindings.
1083 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1084 [EditorBrowsable(EditorBrowsableState.Never)]
1085 ClearOneWayBindings = 1 << 0,
1088 /// Clear TwoWay bindings.
1090 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1091 [EditorBrowsable(EditorBrowsableState.Never)]
1092 ClearTwoWayBindings = 1 << 1,
1095 /// Clear dynamic resource.
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)]
1099 ClearDynamicResource = 1 << 2,
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 RaiseOnEqual = 1 << 3