2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Reflection;
23 using System.Runtime.CompilerServices;
24 using Tizen.NUI.Binding.Internals;
26 namespace Tizen.NUI.Binding
29 /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
31 /// <since_tizen> 9 </since_tizen>
32 public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
35 /// Implements the bound property whose interface is provided by the BindingContext property.
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public static readonly BindableProperty BindingContextProperty =
39 BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
41 var bindableObject = (BindableObject)bindable;
44 bindableObject.bindingContext = newValue;
45 bindableObject.FlushBinding();
47 if (newValue is BindableObject targetBindableObject)
49 targetBindableObject.IsCreateByXaml = true;
53 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
55 if (null != bindable.bindingContext)
57 return bindable.bindingContext;
60 if (bindable is Container container)
62 return container.Parent?.BindingContext;
70 readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
73 object inheritedContext;
75 private object bindingContext;
78 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
80 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public object BindingContext
84 get { return inheritedContext ?? GetValue(BindingContextProperty); }
85 set { SetValue(BindingContextProperty, value); }
88 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
90 SetDynamicResource(property, key, false);
94 /// Raised when a property has changed.
96 /// <since_tizen> 9 </since_tizen>
97 public event PropertyChangedEventHandler PropertyChanged;
99 /// <summary>Copy properties of other ViewStyle to this.</summary>
100 /// <param name="other">The other BindableProperty merge to this.</param>
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 public virtual void CopyFrom(BindableObject other)
104 if (null == other) return;
106 Type type1 = this.GetType();
107 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
109 Type type2 = other.GetType();
110 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
112 if (null != nameToBindableProperty1)
114 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
116 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
118 if (null != bindableProperty)
120 object value = other.GetValue(bindableProperty);
124 SetValue(keyValuePair.Value, value);
132 /// Raised whenever the BindingContext property changes.
134 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public event EventHandler BindingContextChanged;
138 internal void ClearValue(BindableProperty property, bool fromStyle)
140 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
144 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
146 /// <param name="property">The BindableProperty to clear</param>
147 internal void ClearValue(BindableProperty property)
149 ClearValue(property, fromStyle: false, checkAccess: true);
153 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
155 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
156 internal void ClearValue(BindablePropertyKey propertyKey)
158 if (propertyKey == null)
159 throw new ArgumentNullException(nameof(propertyKey));
161 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
165 /// Return true if the target property exists and has been set.
167 /// <param name="targetProperty">The target property</param>
168 /// <returns>return true if the target property exists and has been set</returns>
169 internal bool IsSet(BindableProperty targetProperty)
171 if (targetProperty == null)
172 throw new ArgumentNullException(nameof(targetProperty));
174 var bpcontext = GetContext(targetProperty);
175 return bpcontext != null
176 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
180 /// Returns the value that is contained the BindableProperty.
182 /// <param name="property">The BindableProperty for which to get the value.</param>
183 /// <returns>The value that is contained the BindableProperty</returns>
184 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
185 [EditorBrowsable(EditorBrowsableState.Never)]
186 public object GetValue(BindableProperty property)
188 if (property == null)
189 throw new ArgumentNullException(nameof(property));
191 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
194 return property.DefaultValue;
196 return context.Value;
200 /// Raised when a property is about to change.
202 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public event PropertyChangingEventHandler PropertyChanging;
207 /// Removes a previously set binding.
209 /// <param name="property">The BindableProperty from which to remove bindings.</param>
210 internal void RemoveBinding(BindableProperty property)
212 if (property == null)
213 throw new ArgumentNullException(nameof(property));
215 BindablePropertyContext context = GetContext(property);
216 if (context == null || context.Binding == null)
219 RemoveBinding(property, context);
223 /// Assigns a binding to a property.
225 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
226 /// <param name="binding">The binding to set.</param>
227 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
228 [EditorBrowsable(EditorBrowsableState.Never)]
229 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
231 SetBinding(targetProperty, binding, false);
234 private bool isCreateByXaml = false;
235 /// Only used by the IL of xaml, will never changed to not hidden.
236 [EditorBrowsable(EditorBrowsableState.Never)]
237 public virtual bool IsCreateByXaml
241 return isCreateByXaml;
245 isCreateByXaml = value;
250 /// Sets the value of the specified property.
252 /// <param name="property">The BindableProperty on which to assign a value.</param>
253 /// <param name="value">The value to set.</param>
254 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
255 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
256 [EditorBrowsable(EditorBrowsableState.Never)]
257 public void SetValue(BindableProperty property, object value)
259 if (true == isCreateByXaml)
261 SetValue(property, value, false, true);
265 if (null == property)
267 throw new ArgumentNullException(nameof(property));
270 if (null == property.DefaultValueCreator)
272 BindablePropertyContext context = GetOrCreateContext(property);
275 context.Value = value;
279 property.PropertyChanged?.Invoke(this, null, value);
281 OnPropertyChanged(property.PropertyName);
282 OnPropertyChangedWithData(property);
286 internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
288 if (property == null)
289 throw new ArgumentNullException(nameof(property));
291 if (true == isCreateByXaml)
293 if (property.IsReadOnly)
294 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
296 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
297 SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
301 property.PropertyChanged?.Invoke(this, null, value);
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))
581 IsCreateByXaml = true;
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 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
757 object context = bindable.inheritedContext;
758 var oldBinding = oldBindingBase as Binding;
759 var newBinding = newBindingBase as Binding;
761 if (context == null && oldBinding != null)
762 context = oldBinding.Context;
763 if (context != null && newBinding != null)
764 newBinding.Context = context;
767 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
769 if (property == null)
770 throw new ArgumentNullException(nameof(property));
772 if (checkAccess && property.IsReadOnly)
773 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
775 BindablePropertyContext bpcontext = GetContext(property);
776 if (bpcontext == null)
779 if (fromStyle && !CanBeSetFromStyle(property))
782 object original = bpcontext.Value;
784 object newValue = property.GetDefaultValue(this);
786 bool same = Equals(original, newValue);
789 property.PropertyChanging?.Invoke(this, original, newValue);
791 OnPropertyChanging(property.PropertyName);
794 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
795 bpcontext.Value = newValue;
796 if (property.DefaultValueCreator == null)
797 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
799 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
803 OnPropertyChanged(property.PropertyName);
804 OnPropertyChangedWithData(property);
805 property.PropertyChanged?.Invoke(this, original, newValue);
809 [MethodImpl(MethodImplOptions.AggressiveInlining)]
810 BindablePropertyContext CreateAndAddContext(BindableProperty property)
812 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
814 if (property.DefaultValueCreator == null)
815 context.Attributes = BindableContextAttributes.IsDefaultValue;
817 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
819 properties.Add(context);
823 [MethodImpl(MethodImplOptions.AggressiveInlining)]
824 BindablePropertyContext GetContext(BindableProperty property)
826 List<BindablePropertyContext> propertyList = properties;
828 for (var i = 0; i < propertyList.Count; i++)
830 BindablePropertyContext context = propertyList[i];
831 if (ReferenceEquals(context.Property, property))
838 [MethodImpl(MethodImplOptions.AggressiveInlining)]
839 BindablePropertyContext GetOrCreateContext(BindableProperty property)
841 BindablePropertyContext context = GetContext(property);
844 context = CreateAndAddContext(property);
846 else if (property.DefaultValueCreator != null)
848 context.Value = property.DefaultValueCreator(this); //Update Value from dali
854 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
856 context.Binding.Unapply();
858 property.BindingChanging?.Invoke(this, context.Binding, null);
860 context.Binding = null;
863 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
865 if (property == null)
866 throw new ArgumentNullException(nameof(property));
868 if (checkAccess && property.IsReadOnly)
869 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
871 if (fromStyle && !CanBeSetFromStyle(property))
874 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
875 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
879 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
881 object original = context.Value;
882 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
883 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
884 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
885 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
887 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
888 if (!silent && (!same || raiseOnEqual))
890 property.PropertyChanging?.Invoke(this, original, value);
892 OnPropertyChanging(property.PropertyName);
895 if (!same || raiseOnEqual)
897 context.Value = value;
900 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
901 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
903 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
904 RemoveDynamicResource(property);
906 BindingBase binding = context.Binding;
909 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
911 RemoveBinding(property, context);
916 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
920 if ((!same || raiseOnEqual))
922 property.PropertyChanged?.Invoke(this, original, value);
924 if (binding != null && !currentlyApplying)
931 OnPropertyChanged(property.PropertyName);
933 if (null != propertyGroup)
935 foreach (var relativeProperty in propertyGroup)
937 if (relativeProperty != property)
939 var relativeContext = GetOrCreateContext(relativeProperty);
940 var relativeBinding = relativeContext.Binding;
942 if (null != relativeBinding)
945 relativeBinding.Apply(true);
949 OnPropertyChanged(relativeProperty.PropertyName);
954 else if (true == same && true == forceSendChangeSignal)
956 if (binding != null && !currentlyApplying)
963 OnPropertyChanged(property.PropertyName);
965 if (null != propertyGroup)
967 foreach (var relativeProperty in propertyGroup)
969 if (relativeProperty != property)
971 var relativeContext = GetOrCreateContext(relativeProperty);
972 var relativeBinding = relativeContext.Binding;
974 if (null != relativeBinding)
977 relativeBinding.Apply(true);
981 OnPropertyChanged(relativeProperty.PropertyName);
987 OnPropertyChangedWithData(property);
991 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
992 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
995 enum BindableContextAttributes
997 IsManuallySet = 1 << 0,
999 IsDynamicResource = 1 << 2,
1000 IsSetFromStyle = 1 << 3,
1001 IsDefaultValue = 1 << 4,
1002 IsDefaultValueCreated = 1 << 5,
1005 class BindablePropertyContext
1007 public BindableContextAttributes Attributes;
1008 public BindingBase Binding;
1009 public Queue<SetValueArgs> DelayedSetters;
1010 public BindableProperty Property;
1011 public object Value;
1015 internal enum SetValuePrivateFlags
1018 CheckAccess = 1 << 0,
1020 ManuallySet = 1 << 2,
1023 Default = CheckAccess
1028 public readonly SetValueFlags Attributes;
1029 public readonly BindablePropertyContext Context;
1030 public readonly bool CurrentlyApplying;
1031 public readonly BindableProperty Property;
1032 public readonly object Value;
1034 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1036 Property = property;
1039 CurrentlyApplying = currentlyApplying;
1040 Attributes = attributes;
1044 internal void AddChildBindableObject(BindableObject child)
1048 children.Add(child);
1049 child.FlushBinding();
1053 internal void RemoveChildBindableObject(BindableObject child)
1055 children.Remove(child);
1058 private List<BindableObject> children = new List<BindableObject>();
1060 private void FlushBinding()
1062 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1063 OnBindingContextChanged();
1065 foreach (var child in children)
1067 child.FlushBinding();
1073 namespace Tizen.NUI.Binding.Internals
1076 /// SetValueFlags. For internal use.
1079 [EditorBrowsable(EditorBrowsableState.Never)]
1080 public enum SetValueFlags
1085 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1086 [EditorBrowsable(EditorBrowsableState.Never)]
1090 /// Clear OneWay bindings.
1092 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1093 [EditorBrowsable(EditorBrowsableState.Never)]
1094 ClearOneWayBindings = 1 << 0,
1097 /// Clear TwoWay bindings.
1099 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1100 [EditorBrowsable(EditorBrowsableState.Never)]
1101 ClearTwoWayBindings = 1 << 1,
1104 /// Clear dynamic resource.
1106 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1107 [EditorBrowsable(EditorBrowsableState.Never)]
1108 ClearDynamicResource = 1 << 2,
1113 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1114 [EditorBrowsable(EditorBrowsableState.Never)]
1115 RaiseOnEqual = 1 << 3