2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Reflection;
23 using System.Runtime.CompilerServices;
24 using Tizen.NUI.Binding.Internals;
26 namespace Tizen.NUI.Binding
29 /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
31 /// <since_tizen> 9 </since_tizen>
32 public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
35 /// Implements the bound property whose interface is provided by the BindingContext property.
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public static readonly BindableProperty BindingContextProperty =
39 BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
41 var bindableObject = (BindableObject)bindable;
44 bindableObject.bindingContext = newValue;
45 bindableObject.FlushBinding();
47 if (newValue is BindableObject targetBindableObject)
49 targetBindableObject.IsBinded = true;
53 defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
55 if (null != bindable.bindingContext)
57 return bindable.bindingContext;
60 if (bindable is Container container)
62 return container.Parent?.BindingContext;
70 readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
73 object inheritedContext;
75 private object bindingContext;
78 /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
80 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public object BindingContext
84 get { return inheritedContext ?? GetValue(BindingContextProperty); }
85 set { SetValue(BindingContextProperty, value); }
88 void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
90 SetDynamicResource(property, key, false);
94 /// Raised when a property has changed.
96 /// <since_tizen> 9 </since_tizen>
97 public event PropertyChangedEventHandler PropertyChanged;
99 /// <summary>Copy properties of other ViewStyle to this.</summary>
100 /// <param name="other">The other BindableProperty merge to this.</param>
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 public virtual void CopyFrom(BindableObject other)
104 if (null == other) return;
106 Type type1 = this.GetType();
107 BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
109 Type type2 = other.GetType();
110 BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
112 if (null != nameToBindableProperty1)
114 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
116 nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
118 if (null != bindableProperty)
120 object value = other.GetValue(bindableProperty);
124 SetValue(keyValuePair.Value, value);
132 /// Raised whenever the BindingContext property changes.
134 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
135 [EditorBrowsable(EditorBrowsableState.Never)]
136 public event EventHandler BindingContextChanged;
138 internal void ClearValue(BindableProperty property, bool fromStyle)
140 ClearValue(property, fromStyle: fromStyle, checkAccess: true);
144 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
146 /// <param name="property">The BindableProperty to clear</param>
147 internal void ClearValue(BindableProperty property)
149 ClearValue(property, fromStyle: false, checkAccess: true);
153 /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
155 /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
156 internal void ClearValue(BindablePropertyKey propertyKey)
158 if (propertyKey == null)
159 throw new ArgumentNullException(nameof(propertyKey));
161 ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
165 /// Return true if the target property exists and has been set.
167 /// <param name="targetProperty">The target property</param>
168 /// <returns>return true if the target property exists and has been set</returns>
169 internal bool IsSet(BindableProperty targetProperty)
171 if (targetProperty == null)
172 throw new ArgumentNullException(nameof(targetProperty));
174 var bpcontext = GetContext(targetProperty);
175 return bpcontext != null
176 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
180 /// Returns the value that is contained the BindableProperty.
182 /// <param name="property">The BindableProperty for which to get the value.</param>
183 /// <returns>The value that is contained the BindableProperty</returns>
184 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
185 [EditorBrowsable(EditorBrowsableState.Never)]
186 public object GetValue(BindableProperty property)
188 if (property == null)
189 throw new ArgumentNullException(nameof(property));
191 BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
194 return property.DefaultValue;
196 return context.Value;
200 /// Raised when a property is about to change.
202 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public event PropertyChangingEventHandler PropertyChanging;
207 /// Removes a previously set binding.
209 /// <param name="property">The BindableProperty from which to remove bindings.</param>
210 internal void RemoveBinding(BindableProperty property)
212 if (property == null)
213 throw new ArgumentNullException(nameof(property));
215 BindablePropertyContext context = GetContext(property);
216 if (context == null || context.Binding == null)
219 RemoveBinding(property, context);
223 /// Assigns a binding to a property.
225 /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
226 /// <param name="binding">The binding to set.</param>
227 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
228 [EditorBrowsable(EditorBrowsableState.Never)]
229 public void SetBinding(BindableProperty targetProperty, BindingBase binding)
231 SetBinding(targetProperty, binding, false);
234 private bool isCreateByXaml = false;
235 /// Only used by the IL of xaml, will never changed to not hidden.
236 [EditorBrowsable(EditorBrowsableState.Never)]
237 public virtual bool IsCreateByXaml
241 return isCreateByXaml;
245 isCreateByXaml = value;
250 /// Sets the value of the specified property.
252 /// <param name="property">The BindableProperty on which to assign a value.</param>
253 /// <param name="value">The value to set.</param>
254 /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
255 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
256 [EditorBrowsable(EditorBrowsableState.Never)]
257 public void SetValue(BindableProperty property, object value)
259 if (true == IsBinded)
261 SetValue(property, value, false, true);
265 if (null == property)
267 throw new ArgumentNullException(nameof(property));
270 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))
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 bool IsBinded
761 static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
763 object context = bindable.inheritedContext;
764 var oldBinding = oldBindingBase as Binding;
765 var newBinding = newBindingBase as Binding;
767 if (context == null && oldBinding != null)
768 context = oldBinding.Context;
769 if (context != null && newBinding != null)
770 newBinding.Context = context;
773 void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
775 if (property == null)
776 throw new ArgumentNullException(nameof(property));
778 if (checkAccess && property.IsReadOnly)
779 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
781 BindablePropertyContext bpcontext = GetContext(property);
782 if (bpcontext == null)
785 if (fromStyle && !CanBeSetFromStyle(property))
788 object original = bpcontext.Value;
790 object newValue = property.GetDefaultValue(this);
792 bool same = Equals(original, newValue);
795 property.PropertyChanging?.Invoke(this, original, newValue);
797 OnPropertyChanging(property.PropertyName);
800 bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
801 bpcontext.Value = newValue;
802 if (property.DefaultValueCreator == null)
803 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
805 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
809 OnPropertyChanged(property.PropertyName);
810 OnPropertyChangedWithData(property);
811 property.PropertyChanged?.Invoke(this, original, newValue);
815 [MethodImpl(MethodImplOptions.AggressiveInlining)]
816 BindablePropertyContext CreateAndAddContext(BindableProperty property)
818 var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
820 if (property.DefaultValueCreator == null)
821 context.Attributes = BindableContextAttributes.IsDefaultValue;
823 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
825 properties.Add(context);
829 [MethodImpl(MethodImplOptions.AggressiveInlining)]
830 BindablePropertyContext GetContext(BindableProperty property)
832 List<BindablePropertyContext> propertyList = properties;
834 for (var i = 0; i < propertyList.Count; i++)
836 BindablePropertyContext context = propertyList[i];
837 if (ReferenceEquals(context.Property, property))
844 [MethodImpl(MethodImplOptions.AggressiveInlining)]
845 BindablePropertyContext GetOrCreateContext(BindableProperty property)
847 BindablePropertyContext context = GetContext(property);
850 context = CreateAndAddContext(property);
852 else if (property.DefaultValueCreator != null)
854 context.Value = property.DefaultValueCreator(this); //Update Value from dali
860 void RemoveBinding(BindableProperty property, BindablePropertyContext context)
862 context.Binding.Unapply();
864 property.BindingChanging?.Invoke(this, context.Binding, null);
866 context.Binding = null;
869 internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
871 if (property == null)
872 throw new ArgumentNullException(nameof(property));
874 if (checkAccess && property.IsReadOnly)
875 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
877 if (fromStyle && !CanBeSetFromStyle(property))
880 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
881 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
885 void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
887 object original = context.Value;
888 bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
889 bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
890 bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
891 bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
893 bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
894 if (!silent && (!same || raiseOnEqual))
896 property.PropertyChanging?.Invoke(this, original, value);
898 OnPropertyChanging(property.PropertyName);
901 if (!same || raiseOnEqual)
903 context.Value = value;
906 context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
907 context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
909 if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
910 RemoveDynamicResource(property);
912 BindingBase binding = context.Binding;
915 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
917 RemoveBinding(property, context);
922 PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
926 if ((!same || raiseOnEqual))
928 property.PropertyChanged?.Invoke(this, original, value);
930 if (binding != null && !currentlyApplying)
937 OnPropertyChanged(property.PropertyName);
939 if (null != propertyGroup)
941 foreach (var relativeProperty in propertyGroup)
943 if (relativeProperty != property)
945 var relativeContext = GetOrCreateContext(relativeProperty);
946 var relativeBinding = relativeContext.Binding;
948 if (null != relativeBinding)
951 relativeBinding.Apply(true);
955 OnPropertyChanged(relativeProperty.PropertyName);
960 else if (true == same && true == forceSendChangeSignal)
962 if (binding != null && !currentlyApplying)
969 OnPropertyChanged(property.PropertyName);
971 if (null != propertyGroup)
973 foreach (var relativeProperty in propertyGroup)
975 if (relativeProperty != property)
977 var relativeContext = GetOrCreateContext(relativeProperty);
978 var relativeBinding = relativeContext.Binding;
980 if (null != relativeBinding)
983 relativeBinding.Apply(true);
987 OnPropertyChanged(relativeProperty.PropertyName);
993 OnPropertyChangedWithData(property);
997 private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
998 = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
1001 enum BindableContextAttributes
1003 IsManuallySet = 1 << 0,
1004 IsBeingSet = 1 << 1,
1005 IsDynamicResource = 1 << 2,
1006 IsSetFromStyle = 1 << 3,
1007 IsDefaultValue = 1 << 4,
1008 IsDefaultValueCreated = 1 << 5,
1011 class BindablePropertyContext
1013 public BindableContextAttributes Attributes;
1014 public BindingBase Binding;
1015 public Queue<SetValueArgs> DelayedSetters;
1016 public BindableProperty Property;
1017 public object Value;
1021 internal enum SetValuePrivateFlags
1024 CheckAccess = 1 << 0,
1026 ManuallySet = 1 << 2,
1029 Default = CheckAccess
1034 public readonly SetValueFlags Attributes;
1035 public readonly BindablePropertyContext Context;
1036 public readonly bool CurrentlyApplying;
1037 public readonly BindableProperty Property;
1038 public readonly object Value;
1040 public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1042 Property = property;
1045 CurrentlyApplying = currentlyApplying;
1046 Attributes = attributes;
1050 internal void AddChildBindableObject(BindableObject child)
1054 children.Add(child);
1055 child.FlushBinding();
1059 internal void RemoveChildBindableObject(BindableObject child)
1061 children.Remove(child);
1064 private List<BindableObject> children = new List<BindableObject>();
1066 private void FlushBinding()
1068 ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1069 OnBindingContextChanged();
1071 foreach (var child in children)
1073 child.FlushBinding();
1079 namespace Tizen.NUI.Binding.Internals
1082 /// SetValueFlags. For internal use.
1085 [EditorBrowsable(EditorBrowsableState.Never)]
1086 public enum SetValueFlags
1091 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1092 [EditorBrowsable(EditorBrowsableState.Never)]
1096 /// Clear OneWay bindings.
1098 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1099 [EditorBrowsable(EditorBrowsableState.Never)]
1100 ClearOneWayBindings = 1 << 0,
1103 /// Clear TwoWay bindings.
1105 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1106 [EditorBrowsable(EditorBrowsableState.Never)]
1107 ClearTwoWayBindings = 1 << 1,
1110 /// Clear dynamic resource.
1112 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1113 [EditorBrowsable(EditorBrowsableState.Never)]
1114 ClearDynamicResource = 1 << 2,
1119 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1120 [EditorBrowsable(EditorBrowsableState.Never)]
1121 RaiseOnEqual = 1 << 3