[NUI][Xaml] Fix issue that BindingContext can't be used in C# code
authorFang Xiaohui <xiaohui.fang@samsung.com>
Tue, 1 Jun 2021 05:50:06 +0000 (13:50 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 1 Jun 2021 08:03:31 +0000 (17:03 +0900)
src/Tizen.NUI/src/public/BaseComponents/ViewInternal.cs
src/Tizen.NUI/src/public/BaseComponents/ViewPublicMethods.cs
src/Tizen.NUI/src/public/XamlBinding/BindableObject.cs

index eecd9e9..dcc801e 100755 (executable)
@@ -1013,6 +1013,8 @@ namespace Tizen.NUI.BaseComponents
             Children.Remove(child);
             child.InternalParent = null;
 
+            RemoveChildBindableObject(child);
+
             if (ChildRemoved != null)
             {
                 ChildRemovedEventArgs e = new ChildRemovedEventArgs
index e2bcd01..ba203dc 100755 (executable)
@@ -166,7 +166,8 @@ namespace Tizen.NUI.BaseComponents
                     };
                     ChildAdded(this, e);
                 }
-                BindableObject.SetInheritedBindingContext(child, this?.BindingContext);
+
+                AddChildBindableObject(child);
             }
         }
 
index db70c01..4e662d1 100755 (executable)
@@ -36,14 +36,39 @@ namespace Tizen.NUI.Binding
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public static readonly BindableProperty BindingContextProperty =
-            BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object),
-                                    BindingMode.OneWay, null, BindingContextPropertyChanged, null, null, BindingContextPropertyBindingChanging);
+            BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
+            {
+                var bindableObject = (BindableObject)bindable;
+                if (newValue != null)
+                {
+                    bindableObject.bindingContext = newValue;
+                    bindableObject.FlushBinding();
+                }
+            }),
+            defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
+            {
+                if (null != bindable.bindingContext)
+                {
+                    return bindable.bindingContext;
+                }
+
+                if (bindable is Container container)
+                {
+                    return container.Parent?.BindingContext;
+                }
+                else
+                {
+                    return null;
+                }
+            }));
 
         readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
 
         bool applying;
         object inheritedContext;
 
+        private object bindingContext;
+
         /// <summary>
         /// Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.
         /// </summary>
@@ -359,7 +384,7 @@ namespace Tizen.NUI.Binding
         /// Method that is called when a bound property is changed.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected virtual void OnPropertyChangedWithData(BindableProperty property) { }
+        protected virtual void OnPropertyChangedWithData(BindableProperty prop) { }
 
         /// <summary>
         /// Unapplies all previously set bindings.
@@ -704,13 +729,6 @@ namespace Tizen.NUI.Binding
                 newBinding.Context = context;
         }
 
-        static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
-        {
-            bindable.inheritedContext = null;
-            bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
-            bindable.OnBindingContextChanged();
-        }
-
         void ClearValue(BindableProperty property, bool fromStyle, bool checkAccess)
         {
             if (property == null)
@@ -940,6 +958,33 @@ namespace Tizen.NUI.Binding
                 Attributes = attributes;
             }
         }
+
+        internal void AddChildBindableObject(BindableObject child)
+        {
+            if (null != child)
+            {
+                children.Add(child);
+                child.FlushBinding();
+            }
+        }
+
+        internal void RemoveChildBindableObject(BindableObject child)
+        {
+            children.Remove(child);
+        }
+
+        private List<BindableObject> children = new List<BindableObject>();
+
+        private void FlushBinding()
+        {
+            ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
+            OnBindingContextChanged();
+
+            foreach (var child in children)
+            {
+                child.FlushBinding();
+            }
+        }
     }
 }