[NUI] Provide method so that user can register properties to group, then they can...
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / BindableObject.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
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;
25
26 namespace Tizen.NUI.Binding
27 {
28     /// <summary>
29     /// Provides a mechanism by which application developers can propagate changes that are made to data in one object to another.
30     /// </summary>
31     /// <since_tizen> 9 </since_tizen>
32     public abstract class BindableObject : INotifyPropertyChanged, IDynamicResourceHandler
33     {
34         /// <summary>
35         /// Implements the bound property whose interface is provided by the BindingContext property.
36         /// </summary>
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) =>
40             {
41                 var bindableObject = (BindableObject)bindable;
42                 if (newValue != null)
43                 {
44                     bindableObject.bindingContext = newValue;
45                     bindableObject.FlushBinding();
46                 }
47             }),
48             defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
49             {
50                 if (null != bindable.bindingContext)
51                 {
52                     return bindable.bindingContext;
53                 }
54
55                 if (bindable is Container container)
56                 {
57                     return container.Parent?.BindingContext;
58                 }
59                 else
60                 {
61                     return null;
62                 }
63             }));
64
65         readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
66
67         bool applying;
68         object inheritedContext;
69
70         private object bindingContext;
71
72         /// <summary>
73         /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
74         /// </summary>
75         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public object BindingContext
78         {
79             get { return inheritedContext ?? GetValue(BindingContextProperty); }
80             set { SetValue(BindingContextProperty, value); }
81         }
82
83         void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
84         {
85             SetDynamicResource(property, key, false);
86         }
87
88         /// <summary>
89         /// Raised when a property has changed.
90         /// </summary>
91         /// <since_tizen> 9 </since_tizen>
92         public event PropertyChangedEventHandler PropertyChanged;
93
94         /// <summary>Copy properties of other ViewStyle to this.</summary>
95         /// <param name="other">The other BindableProperty merge to this.</param>
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public virtual void CopyFrom(BindableObject other)
98         {
99             if (null == other) return;
100
101             Type type1 = this.GetType();
102             BindableProperty.GetBindablePropertysOfType(type1, out var nameToBindableProperty1);
103
104             Type type2 = other.GetType();
105             BindableProperty.GetBindablePropertysOfType(type2, out var nameToBindableProperty2);
106
107             if (null != nameToBindableProperty1)
108             {
109                 foreach (KeyValuePair<string, BindableProperty> keyValuePair in nameToBindableProperty1)
110                 {
111                     nameToBindableProperty2.TryGetValue(keyValuePair.Key, out var bindableProperty);
112
113                     if (null != bindableProperty)
114                     {
115                         object value = other.GetValue(bindableProperty);
116
117                         if (null != value)
118                         {
119                             SetValue(keyValuePair.Value, value);
120                         }
121                     }
122                 }
123             }
124         }
125
126         /// <summary>
127         /// Raised whenever the BindingContext property changes.
128         /// </summary>
129         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
130         [EditorBrowsable(EditorBrowsableState.Never)]
131         public event EventHandler BindingContextChanged;
132
133         internal void ClearValue(BindableProperty property, bool fromStyle)
134         {
135             ClearValue(property, fromStyle: fromStyle, checkAccess: true);
136         }
137
138         /// <summary>
139         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue.
140         /// </summary>
141         /// <param name="property">The BindableProperty to clear</param>
142         internal void ClearValue(BindableProperty property)
143         {
144             ClearValue(property, fromStyle: false, checkAccess: true);
145         }
146
147         /// <summary>
148         /// Clears any value set by Tizen.NUI.Xaml.BindableObject.SetValue for the property that is identified by propertyKey.
149         /// </summary>
150         /// <param name="propertyKey">The BindablePropertyKey that identifies the BindableProperty to clear.</param>
151         internal void ClearValue(BindablePropertyKey propertyKey)
152         {
153             if (propertyKey == null)
154                 throw new ArgumentNullException(nameof(propertyKey));
155
156             ClearValue(propertyKey.BindableProperty, fromStyle: false, checkAccess: false);
157         }
158
159         /// <summary>
160         /// Return true if the target property exists and has been set.
161         /// </summary>
162         /// <param name="targetProperty">The target property</param>
163         /// <returns>return true if the target property exists and has been set</returns>
164         internal bool IsSet(BindableProperty targetProperty)
165         {
166             if (targetProperty == null)
167                 throw new ArgumentNullException(nameof(targetProperty));
168
169             var bpcontext = GetContext(targetProperty);
170             return bpcontext != null
171                 && (bpcontext.Attributes & BindableContextAttributes.IsDefaultValue) == 0;
172         }
173
174         /// <summary>
175         /// Returns the value that is contained the BindableProperty.
176         /// </summary>
177         /// <param name="property">The BindableProperty for which to get the value.</param>
178         /// <returns>The value that is contained the BindableProperty</returns>
179         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
180         [EditorBrowsable(EditorBrowsableState.Never)]
181         public object GetValue(BindableProperty property)
182         {
183             if (property == null)
184                 throw new ArgumentNullException(nameof(property));
185
186             BindablePropertyContext context = property.DefaultValueCreator != null ? GetOrCreateContext(property) : GetContext(property);
187
188             if (context == null)
189                 return property.DefaultValue;
190
191             return context.Value;
192         }
193
194         /// <summary>
195         /// Raised when a property is about to change.
196         /// </summary>
197         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
198         [EditorBrowsable(EditorBrowsableState.Never)]
199         public event PropertyChangingEventHandler PropertyChanging;
200
201         /// <summary>
202         /// Removes a previously set binding.
203         /// </summary>
204         /// <param name="property">The BindableProperty from which to remove bindings.</param>
205         internal void RemoveBinding(BindableProperty property)
206         {
207             if (property == null)
208                 throw new ArgumentNullException(nameof(property));
209
210             BindablePropertyContext context = GetContext(property);
211             if (context == null || context.Binding == null)
212                 return;
213
214             RemoveBinding(property, context);
215         }
216
217         /// <summary>
218         /// Assigns a binding to a property.
219         /// </summary>
220         /// <param name="targetProperty">The BindableProperty on which to set a binding.</param>
221         /// <param name="binding">The binding to set.</param>
222         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
223         [EditorBrowsable(EditorBrowsableState.Never)]
224         public void SetBinding(BindableProperty targetProperty, BindingBase binding)
225         {
226             SetBinding(targetProperty, binding, false);
227         }
228
229         private bool isCreateByXaml = false;
230         /// Only used by the IL of xaml, will never changed to not hidden.
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         public virtual bool IsCreateByXaml
233         {
234             get
235             {
236                 return isCreateByXaml;
237             }
238             set
239             {
240                 isCreateByXaml = value;
241             }
242         }
243
244         /// <summary>
245         /// Sets the value of the specified property.
246         /// </summary>
247         /// <param name="property">The BindableProperty on which to assign a value.</param>
248         /// <param name="value">The value to set.</param>
249         /// <exception cref="ArgumentNullException"> Thrown when property is null. </exception>
250         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         public void SetValue(BindableProperty property, object value)
253         {
254             if (true == isCreateByXaml)
255             {
256                 SetValue(property, value, false, true);
257             }
258             else
259             {
260                 if (null == property)
261                 {
262                     throw new ArgumentNullException(nameof(property));
263                 }
264                 property.PropertyChanged?.Invoke(this, null, value);
265
266                 OnPropertyChanged(property.PropertyName);
267                 OnPropertyChangedWithData(property);
268             }
269         }
270
271         internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
272         {
273             if (property == null)
274                 throw new ArgumentNullException(nameof(property));
275
276             if (true == isCreateByXaml)
277             {
278                 if (property.IsReadOnly)
279                     throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
280
281                 SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
282                     SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
283             }
284             else
285             {
286                 property.PropertyChanged?.Invoke(this, null, value);
287             }
288         }
289
290         /// <summary>
291         /// Sets the value of the propertyKey.
292         /// </summary>
293         /// <param name="propertyKey">The BindablePropertyKey on which to assign a value.</param>
294         /// <param name="value">The value to set.</param>
295         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
296         [EditorBrowsable(EditorBrowsableState.Never)]
297         public void SetValue(BindablePropertyKey propertyKey, object value)
298         {
299             if (propertyKey == null)
300                 throw new ArgumentNullException(nameof(propertyKey));
301
302             SetValue(propertyKey.BindableProperty, value, false, false);
303         }
304
305         /// <summary>
306         /// Set the inherited context to a neated element.
307         /// </summary>
308         /// <param name="bindable">The object on which to set the inherited binding context.</param>
309         /// <param name="value">The inherited context to set.</param>
310         /// <exception cref="ArgumentNullException"> Thrown when bindable is null. </exception>
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         public static void SetInheritedBindingContext(BindableObject bindable, object value)
313         {
314             if (null == bindable)
315             {
316                 throw new ArgumentNullException(nameof(bindable));
317             }
318
319             BindablePropertyContext bpContext = bindable.GetContext(BindingContextProperty);
320             if (bpContext != null && ((bpContext.Attributes & BindableContextAttributes.IsManuallySet) != 0))
321                 return;
322
323             object oldContext = bindable.inheritedContext;
324
325             if (ReferenceEquals(oldContext, value))
326                 return;
327
328             if (bpContext != null && oldContext == null)
329                 oldContext = bpContext.Value;
330
331             if (bpContext != null && bpContext.Binding != null)
332             {
333                 bpContext.Binding.Context = value;
334                 bindable.inheritedContext = null;
335             }
336             else
337             {
338                 bindable.inheritedContext = value;
339             }
340
341             bindable.ApplyBindings(skipBindingContext: false, fromBindingContextChanged: true);
342             bindable.OnBindingContextChanged();
343         }
344
345         /// <summary>
346         /// Register the properties which can effect each other in Binding to same group.
347         /// </summary>
348         [EditorBrowsable(EditorBrowsableState.Never)]
349         public static void RegisterPropertyGroup(BindableProperty property, HashSet<BindableProperty> group)
350         {
351             if (!PropertyToGroup.ContainsKey(property))
352             {
353                 PropertyToGroup.Add(property, group);
354             }
355         }
356         /// <summary>
357         /// Apply the bindings to BindingContext.
358         /// </summary>
359         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
360         [EditorBrowsable(EditorBrowsableState.Never)]
361         protected void ApplyBindings()
362         {
363             ApplyBindings(skipBindingContext: false, fromBindingContextChanged: false);
364         }
365
366         /// <summary>
367         /// Override this method to execute an action when the BindingContext changes.
368         /// </summary>
369         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
370         [EditorBrowsable(EditorBrowsableState.Never)]
371         protected virtual void OnBindingContextChanged()
372         {
373             BindingContextChanged?.Invoke(this, EventArgs.Empty);
374         }
375
376         /// <summary>
377         /// Call this method from a child class to notify that a change happened on a property.
378         /// </summary>
379         /// <param name="propertyName">The name of the property that changed.</param>
380         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
381         [EditorBrowsable(EditorBrowsableState.Never)]
382         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
383             => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
384
385         /// <summary>
386         /// Call this method from a child class to notify that a change is going to happen on a property.
387         /// </summary>
388         /// <param name="propertyName">The name of the property that is changing.</param>
389         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
390         [EditorBrowsable(EditorBrowsableState.Never)]
391         protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
392             => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
393         
394         /// <summary>
395         /// Method that is called when a bound property is changed.
396         /// </summary>
397         [EditorBrowsable(EditorBrowsableState.Never)]
398         protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
399
400         /// <summary>
401         /// Unapplies all previously set bindings.
402         /// </summary>
403         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
404         [EditorBrowsable(EditorBrowsableState.Never)]
405         protected void UnapplyBindings()
406         {
407             for (int i = 0, _propertiesCount = properties.Count; i < _propertiesCount; i++)
408             {
409                 BindablePropertyContext context = properties[i];
410                 if (context.Binding == null)
411                     continue;
412
413                 context.Binding.Unapply();
414             }
415         }
416
417         internal bool GetIsBound(BindableProperty targetProperty)
418         {
419             if (targetProperty == null)
420                 throw new ArgumentNullException(nameof(targetProperty));
421
422             BindablePropertyContext bpcontext = GetContext(targetProperty);
423             return bpcontext != null && bpcontext.Binding != null;
424         }
425
426         /// <summary>
427         /// Returns the value that is contained the BindableProperty.
428         /// </summary>
429         /// <param name="property0">The BindableProperty instance.</param>
430         /// <param name="property1">The BindableProperty instance.</param>
431         /// <returns>The value that is contained the BindableProperty</returns>
432         internal object[] GetValues(BindableProperty property0, BindableProperty property1)
433         {
434             var values = new object[2];
435
436             for (var i = 0; i < properties.Count; i++)
437             {
438                 BindablePropertyContext context = properties[i];
439
440                 if (ReferenceEquals(context.Property, property0))
441                 {
442                     values[0] = context.Value;
443                     property0 = null;
444                 }
445                 else if (ReferenceEquals(context.Property, property1))
446                 {
447                     values[1] = context.Value;
448                     property1 = null;
449                 }
450
451                 if (property0 == null && property1 == null)
452                     return values;
453             }
454
455             if (!ReferenceEquals(property0, null))
456                 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
457             if (!ReferenceEquals(property1, null))
458                 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
459
460             return values;
461         }
462
463         /// <summary>
464         /// Returns the value that is contained the BindableProperty.
465         /// </summary>
466         /// <param name="property0">The BindableProperty instance.</param>
467         /// <param name="property1">The BindableProperty instance.</param>
468         /// <param name="property2">The BindableProperty instance.</param>
469         /// <returns>The value that is contained the BindableProperty</returns>
470         internal object[] GetValues(BindableProperty property0, BindableProperty property1, BindableProperty property2)
471         {
472             var values = new object[3];
473
474             for (var i = 0; i < properties.Count; i++)
475             {
476                 BindablePropertyContext context = properties[i];
477
478                 if (ReferenceEquals(context.Property, property0))
479                 {
480                     values[0] = context.Value;
481                     property0 = null;
482                 }
483                 else if (ReferenceEquals(context.Property, property1))
484                 {
485                     values[1] = context.Value;
486                     property1 = null;
487                 }
488                 else if (ReferenceEquals(context.Property, property2))
489                 {
490                     values[2] = context.Value;
491                     property2 = null;
492                 }
493
494                 if (property0 == null && property1 == null && property2 == null)
495                     return values;
496             }
497
498             if (!ReferenceEquals(property0, null))
499                 values[0] = property0.DefaultValueCreator == null ? property0.DefaultValue : CreateAndAddContext(property0).Value;
500             if (!ReferenceEquals(property1, null))
501                 values[1] = property1.DefaultValueCreator == null ? property1.DefaultValue : CreateAndAddContext(property1).Value;
502             if (!ReferenceEquals(property2, null))
503                 values[2] = property2.DefaultValueCreator == null ? property2.DefaultValue : CreateAndAddContext(property2).Value;
504
505             return values;
506         }
507
508         /// <summary>
509         /// Returns the value that is contained the BindableProperty.
510         /// </summary>
511         /// <param name="properties">The array of the BindableProperty instances</param>
512         /// <returns>The values that is contained the BindableProperty instances.</returns>
513         internal object[] GetValues(params BindableProperty[] properties)
514         {
515             var values = new object[properties.Length];
516             for (var i = 0; i < this.properties.Count; i++)
517             {
518                 var context = this.properties[i];
519                 var index = properties.IndexOf(context.Property);
520                 if (index < 0)
521                     continue;
522                 values[index] = context.Value;
523             }
524             for (var i = 0; i < values.Length; i++)
525             {
526                 if (!ReferenceEquals(values[i], null))
527                     continue;
528                 values[i] = properties[i].DefaultValueCreator == null ? properties[i].DefaultValue : CreateAndAddContext(properties[i]).Value;
529             }
530             return values;
531         }
532
533         internal virtual void OnRemoveDynamicResource(BindableProperty property)
534         {
535         }
536
537         internal virtual void OnSetDynamicResource(BindableProperty property, string key)
538         {
539         }
540
541         internal void RemoveDynamicResource(BindableProperty property)
542         {
543             if (property == null)
544                 throw new ArgumentNullException(nameof(property));
545
546             OnRemoveDynamicResource(property);
547             BindablePropertyContext context = GetOrCreateContext(property);
548             context.Attributes &= ~BindableContextAttributes.IsDynamicResource;
549         }
550
551         internal void SetBinding(BindableProperty targetProperty, BindingBase binding, bool fromStyle)
552         {
553             if (targetProperty == null)
554                 throw new ArgumentNullException(nameof(targetProperty));
555             if (binding == null)
556                 throw new ArgumentNullException(nameof(binding));
557
558             if (fromStyle && !CanBeSetFromStyle(targetProperty))
559                 return;
560
561             IsCreateByXaml = true;
562
563             var context = GetOrCreateContext(targetProperty);
564             if (fromStyle)
565                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
566             else
567                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
568
569             if (context.Binding != null)
570                 context.Binding.Unapply();
571
572             BindingBase oldBinding = context.Binding;
573             context.Binding = binding;
574
575             targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);
576
577             binding.Apply(BindingContext, this, targetProperty);
578         }
579
580         bool CanBeSetFromStyle(BindableProperty property)
581         {
582             var context = GetContext(property);
583             if (context == null)
584                 return true;
585             if ((context.Attributes & BindableContextAttributes.IsSetFromStyle) == BindableContextAttributes.IsSetFromStyle)
586                 return true;
587             if ((context.Attributes & BindableContextAttributes.IsDefaultValue) == BindableContextAttributes.IsDefaultValue)
588                 return true;
589             if ((context.Attributes & BindableContextAttributes.IsDefaultValueCreated) == BindableContextAttributes.IsDefaultValueCreated)
590                 return true;
591             return false;
592         }
593
594         internal void SetDynamicResource(BindableProperty property, string key)
595         {
596             SetDynamicResource(property, key, false);
597         }
598
599         internal void SetDynamicResource(BindableProperty property, string key, bool fromStyle)
600         {
601             if (property == null)
602                 throw new ArgumentNullException(nameof(property));
603             if (string.IsNullOrEmpty(key))
604                 throw new ArgumentNullException(nameof(key));
605             if (fromStyle && !CanBeSetFromStyle(property))
606                 return;
607
608             var context = GetOrCreateContext(property);
609
610             context.Attributes |= BindableContextAttributes.IsDynamicResource;
611             if (fromStyle)
612                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
613             else
614                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
615
616             OnSetDynamicResource(property, key);
617         }
618
619         internal void SetValue(BindableProperty property, object value, bool fromStyle)
620         {
621             SetValue(property, value, fromStyle, true);
622         }
623
624         internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
625         {
626             SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
627         }
628
629         /// <summary>
630         /// For internal use.
631         /// </summary>
632         /// <param name="property">The BindableProperty on which to assign a value.</param>
633         /// <param name="value">The value to set</param>
634         /// <param name="attributes">The set value flag</param>
635         [EditorBrowsable(EditorBrowsableState.Never)]
636         internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
637         {
638             SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
639         }
640
641         internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
642         {
643             bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
644             bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
645             bool silent = (privateAttributes & SetValuePrivateFlags.Silent) != 0;
646             bool fromStyle = (privateAttributes & SetValuePrivateFlags.FromStyle) != 0;
647             bool converted = (privateAttributes & SetValuePrivateFlags.Converted) != 0;
648
649             if (property == null)
650                 throw new ArgumentNullException(nameof(property));
651             if (checkAccess && property.IsReadOnly)
652             {
653                 Debug.WriteLine("Can not set the BindableProperty \"{0}\" because it is readonly.", property.PropertyName);
654                 return;
655             }
656
657             if (!converted && !property.TryConvert(ref value))
658             {
659                 Console.WriteLine($"SetValue : Can not convert {value} to type {property.ReturnType}");
660                 return;
661             }
662
663             if (property.ValidateValue != null && !property.ValidateValue(this, value))
664                 throw new ArgumentException("Value was an invalid value for " + property.PropertyName, nameof(value));
665
666             if (property.CoerceValue != null)
667                 value = property.CoerceValue(this, value);
668
669             BindablePropertyContext context = GetOrCreateContext(property);
670             if (manuallySet)
671             {
672                 context.Attributes |= BindableContextAttributes.IsManuallySet;
673                 context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
674             }
675             else
676             {
677                 context.Attributes &= ~BindableContextAttributes.IsManuallySet;
678             }
679
680             if (fromStyle)
681             {
682                 context.Attributes |= BindableContextAttributes.IsSetFromStyle;
683             }
684             // else omitted on purpose
685
686             bool currentlyApplying = applying;
687
688             if ((context.Attributes & BindableContextAttributes.IsBeingSet) != 0)
689             {
690                 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
691                 if (delayQueue == null)
692                     context.DelayedSetters = delayQueue = new Queue<SetValueArgs>();
693
694                 delayQueue.Enqueue(new SetValueArgs(property, context, value, currentlyApplying, attributes));
695             }
696             else
697             {
698                 context.Attributes |= BindableContextAttributes.IsBeingSet;
699                 SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
700
701                 Queue<SetValueArgs> delayQueue = context.DelayedSetters;
702                 if (delayQueue != null)
703                 {
704                     while (delayQueue.Count > 0)
705                     {
706                         SetValueArgs s = delayQueue.Dequeue();
707                         SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
708                     }
709
710                     context.DelayedSetters = null;
711                 }
712
713                 context.Attributes &= ~BindableContextAttributes.IsBeingSet;
714             }
715         }
716
717         internal void ApplyBindings(bool skipBindingContext, bool fromBindingContextChanged)
718         {
719             var prop = properties.ToArray();
720             for (int i = 0, propLength = prop.Length; i < propLength; i++)
721             {
722                 BindablePropertyContext context = prop[i];
723                 BindingBase binding = context.Binding;
724                 if (binding == null)
725                     continue;
726
727                 if (skipBindingContext && ReferenceEquals(context.Property, BindingContextProperty))
728                     continue;
729
730                 binding.Unapply(fromBindingContextChanged: fromBindingContextChanged);
731                 binding.Apply(BindingContext, this, context.Property, fromBindingContextChanged: fromBindingContextChanged);
732             }
733         }
734
735         static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
736         {
737             object context = bindable.inheritedContext;
738             var oldBinding = oldBindingBase as Binding;
739             var newBinding = newBindingBase as Binding;
740
741             if (context == null && oldBinding != null)
742                 context = oldBinding.Context;
743             if (context != null && newBinding != null)
744                 newBinding.Context = context;
745         }
746
747         void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
748         {
749             if (property == null)
750                 throw new ArgumentNullException(nameof(property));
751
752             if (checkAccess && property.IsReadOnly)
753                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
754
755             BindablePropertyContext bpcontext = GetContext(property);
756             if (bpcontext == null)
757                 return;
758
759             if (fromStyle && !CanBeSetFromStyle(property))
760                 return;
761
762             object original = bpcontext.Value;
763
764             object newValue = property.GetDefaultValue(this);
765
766             bool same = Equals(original, newValue);
767             if (!same)
768             {
769                 property.PropertyChanging?.Invoke(this, original, newValue);
770
771                 OnPropertyChanging(property.PropertyName);
772             }
773
774             bpcontext.Attributes &= ~BindableContextAttributes.IsManuallySet;
775             bpcontext.Value = newValue;
776             if (property.DefaultValueCreator == null)
777                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValue;
778             else
779                 bpcontext.Attributes |= BindableContextAttributes.IsDefaultValueCreated;
780
781             if (!same)
782             {
783                 OnPropertyChanged(property.PropertyName);
784                 OnPropertyChangedWithData(property);
785                 property.PropertyChanged?.Invoke(this, original, newValue);
786             }
787         }
788
789         [MethodImpl(MethodImplOptions.AggressiveInlining)]
790         BindablePropertyContext CreateAndAddContext(BindableProperty property)
791         {
792             var context = new BindablePropertyContext { Property = property, Value = property.DefaultValueCreator != null ? property.DefaultValueCreator(this) : property.DefaultValue };
793
794             if (property.DefaultValueCreator == null)
795                 context.Attributes = BindableContextAttributes.IsDefaultValue;
796             else
797                 context.Attributes = BindableContextAttributes.IsDefaultValueCreated;
798
799             properties.Add(context);
800             return context;
801         }
802
803         [MethodImpl(MethodImplOptions.AggressiveInlining)]
804         BindablePropertyContext GetContext(BindableProperty property)
805         {
806             List<BindablePropertyContext> propertyList = properties;
807
808             for (var i = 0; i < propertyList.Count; i++)
809             {
810                 BindablePropertyContext context = propertyList[i];
811                 if (ReferenceEquals(context.Property, property))
812                     return context;
813             }
814
815             return null;
816         }
817
818         [MethodImpl(MethodImplOptions.AggressiveInlining)]
819         BindablePropertyContext GetOrCreateContext(BindableProperty property)
820         {
821             BindablePropertyContext context = GetContext(property);
822             if (context == null)
823             {
824                 context = CreateAndAddContext(property);
825             }
826             else if (property.DefaultValueCreator != null)
827             {
828                 context.Value = property.DefaultValueCreator(this); //Update Value from dali
829             }//added by xb.teng
830
831             return context;
832         }
833
834         void RemoveBinding(BindableProperty property, BindablePropertyContext context)
835         {
836             context.Binding.Unapply();
837
838             property.BindingChanging?.Invoke(this, context.Binding, null);
839
840             context.Binding = null;
841         }
842
843         void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
844         {
845             if (property == null)
846                 throw new ArgumentNullException(nameof(property));
847
848             if (checkAccess && property.IsReadOnly)
849                 throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
850
851             if (fromStyle && !CanBeSetFromStyle(property))
852                 return;
853
854             SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
855                 (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
856                 false);
857         }
858
859         void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
860         {
861             object original = context.Value;
862             bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
863             bool clearDynamicResources = (attributes & SetValueFlags.ClearDynamicResource) != 0;
864             bool clearOneWayBindings = (attributes & SetValueFlags.ClearOneWayBindings) != 0;
865             bool clearTwoWayBindings = (attributes & SetValueFlags.ClearTwoWayBindings) != 0;
866
867             bool same = ReferenceEquals(context.Property, BindingContextProperty) ? ReferenceEquals(value, original) : Equals(value, original);
868             if (!silent && (!same || raiseOnEqual))
869             {
870                 property.PropertyChanging?.Invoke(this, original, value);
871
872                 OnPropertyChanging(property.PropertyName);
873             }
874
875             if (!same || raiseOnEqual)
876             {
877                 context.Value = value;
878             }
879
880             context.Attributes &= ~BindableContextAttributes.IsDefaultValue;
881             context.Attributes &= ~BindableContextAttributes.IsDefaultValueCreated;
882
883             if ((context.Attributes & BindableContextAttributes.IsDynamicResource) != 0 && clearDynamicResources)
884                 RemoveDynamicResource(property);
885
886             BindingBase binding = context.Binding;
887             if (binding != null)
888             {
889                 if (clearOneWayBindings && binding.GetRealizedMode(property) == BindingMode.OneWay || clearTwoWayBindings && binding.GetRealizedMode(property) == BindingMode.TwoWay)
890                 {
891                     RemoveBinding(property, context);
892                     binding = null;
893                 }
894             }
895
896             PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
897
898             if (!silent)
899             {
900                 if ((!same || raiseOnEqual))
901                 {
902                     property.PropertyChanged?.Invoke(this, original, value);
903
904                     if (binding != null && !currentlyApplying)
905                     {
906                         applying = true;
907                         binding.Apply(true);
908                         applying = false;
909                     }
910
911                     OnPropertyChanged(property.PropertyName);
912
913                     if (null != propertyGroup)
914                     {
915                         foreach (var relativeProperty in propertyGroup)
916                         {
917                             if (relativeProperty != property)
918                             {
919                                 var relativeContext = GetOrCreateContext(relativeProperty);
920                                 var relativeBinding = relativeContext.Binding;
921
922                                 if (null != relativeBinding)
923                                 {
924                                     applying = true;
925                                     relativeBinding.Apply(true);
926                                     applying = false;
927                                 }
928
929                                 OnPropertyChanged(relativeProperty.PropertyName);
930                             }
931                         }
932                     }
933                 }
934                 else if (true == same && true == forceSendChangeSignal)
935                 {
936                     if (binding != null && !currentlyApplying)
937                     {
938                         applying = true;
939                         binding.Apply(true);
940                         applying = false;
941                     }
942
943                     OnPropertyChanged(property.PropertyName);
944
945                     if (null != propertyGroup)
946                     {
947                         foreach (var relativeProperty in propertyGroup)
948                         {
949                             if (relativeProperty != property)
950                             {
951                                 var relativeContext = GetOrCreateContext(relativeProperty);
952                                 var relativeBinding = relativeContext.Binding;
953
954                                 if (null != relativeBinding)
955                                 {
956                                     applying = true;
957                                     relativeBinding.Apply(true);
958                                     applying = false;
959                                 }
960
961                                 OnPropertyChanged(relativeProperty.PropertyName);
962                             }
963                         }
964                     }
965                 }
966
967                 OnPropertyChangedWithData(property);
968             }
969         }
970
971         private static Dictionary<BindableProperty, HashSet<BindableProperty>> PropertyToGroup { get; }
972             = new Dictionary<BindableProperty, HashSet<BindableProperty>>();
973
974         [Flags]
975         enum BindableContextAttributes
976         {
977             IsManuallySet = 1 << 0,
978             IsBeingSet = 1 << 1,
979             IsDynamicResource = 1 << 2,
980             IsSetFromStyle = 1 << 3,
981             IsDefaultValue = 1 << 4,
982             IsDefaultValueCreated = 1 << 5,
983         }
984
985         class BindablePropertyContext
986         {
987             public BindableContextAttributes Attributes;
988             public BindingBase Binding;
989             public Queue<SetValueArgs> DelayedSetters;
990             public BindableProperty Property;
991             public object Value;
992         }
993
994         [Flags]
995         internal enum SetValuePrivateFlags
996         {
997             None = 0,
998             CheckAccess = 1 << 0,
999             Silent = 1 << 1,
1000             ManuallySet = 1 << 2,
1001             FromStyle = 1 << 3,
1002             Converted = 1 << 4,
1003             Default = CheckAccess
1004         }
1005
1006         class SetValueArgs
1007         {
1008             public readonly SetValueFlags Attributes;
1009             public readonly BindablePropertyContext Context;
1010             public readonly bool CurrentlyApplying;
1011             public readonly BindableProperty Property;
1012             public readonly object Value;
1013
1014             public SetValueArgs(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes)
1015             {
1016                 Property = property;
1017                 Context = context;
1018                 Value = value;
1019                 CurrentlyApplying = currentlyApplying;
1020                 Attributes = attributes;
1021             }
1022         }
1023
1024         internal void AddChildBindableObject(BindableObject child)
1025         {
1026             if (null != child)
1027             {
1028                 children.Add(child);
1029                 child.FlushBinding();
1030             }
1031         }
1032
1033         internal void RemoveChildBindableObject(BindableObject child)
1034         {
1035             children.Remove(child);
1036         }
1037
1038         private List<BindableObject> children = new List<BindableObject>();
1039
1040         private void FlushBinding()
1041         {
1042             ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
1043             OnBindingContextChanged();
1044
1045             foreach (var child in children)
1046             {
1047                 child.FlushBinding();
1048             }
1049         }
1050     }
1051 }
1052
1053 namespace Tizen.NUI.Binding.Internals
1054 {
1055     /// <summary>
1056     /// SetValueFlags. For internal use.
1057     /// </summary>
1058     [Flags]
1059     [EditorBrowsable(EditorBrowsableState.Never)]
1060     public enum SetValueFlags
1061     {
1062         /// <summary>
1063         /// None.
1064         /// </summary>
1065         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1066         [EditorBrowsable(EditorBrowsableState.Never)]
1067         None = 0,
1068
1069         /// <summary>
1070         /// Clear OneWay bindings.
1071         /// </summary>
1072         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1073         [EditorBrowsable(EditorBrowsableState.Never)]
1074         ClearOneWayBindings = 1 << 0,
1075
1076         /// <summary>
1077         /// Clear TwoWay bindings.
1078         /// </summary>
1079         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1080         [EditorBrowsable(EditorBrowsableState.Never)]
1081         ClearTwoWayBindings = 1 << 1,
1082
1083         /// <summary>
1084         /// Clear dynamic resource.
1085         /// </summary>
1086         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1087         [EditorBrowsable(EditorBrowsableState.Never)]
1088         ClearDynamicResource = 1 << 2,
1089
1090         /// <summary>
1091         /// Raise or equal.
1092         /// </summary>
1093         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
1094         [EditorBrowsable(EditorBrowsableState.Never)]
1095         RaiseOnEqual = 1 << 3
1096     }
1097 }