[NUI] Make BindableProperty of ItemsSource (#3160)
authorSangHyeon Jade Lee <sh10233.lee@samsung.com>
Fri, 4 Jun 2021 10:31:22 +0000 (19:31 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 8 Jun 2021 05:35:32 +0000 (14:35 +0900)
src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs

index 062aa13..35d639e 100755 (executable)
@@ -100,6 +100,57 @@ namespace Tizen.NUI.Components
                     return colView.selectionMode;
                 });
 
+        /// <summary>
+        /// Binding Property of items data source.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty ItemsSourceProperty =
+            BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(CollectionView), null,
+                propertyChanged: (bindable, oldValue, newValue) =>
+                {
+                    var colView = (CollectionView)bindable;
+                    oldValue = colView.itemsSource;
+
+                    if (oldValue != null)
+                    {
+                        // Clearing old data!
+                        if (oldValue is INotifyCollectionChanged prevNotifyCollectionChanged)
+                        {
+                            prevNotifyCollectionChanged.CollectionChanged -= colView.CollectionChanged;
+                        }
+                        if (colView.selectedItem != null) colView.selectedItem = null;
+                        colView.selectedItems?.Clear();
+                    }
+
+                    colView.itemsSource = (IEnumerable)newValue;
+
+                    if (newValue == null)
+                    {
+                        colView.InternalItemSource?.Dispose();
+                        colView.InternalItemSource = null;
+                        colView.itemsLayouter?.Clear();
+                        colView.ClearCache();
+                        return;
+                    }
+                    if (newValue is INotifyCollectionChanged newNotifyCollectionChanged)
+                    {
+                        newNotifyCollectionChanged.CollectionChanged += colView.CollectionChanged;
+                    }
+
+                    colView.InternalItemSource?.Dispose();
+                    colView.InternalItemSource = ItemsSourceFactory.Create(colView);
+
+                    if (colView.itemsLayouter == null) return;
+
+                    colView.needInitalizeLayouter = true;
+                    colView.Init();
+                },
+                defaultValueCreator: (bindable) =>
+                {
+                    var colView = (CollectionView)bindable;
+                    return colView.itemsSource;
+                });
+
 
         private static readonly IList<object> selectEmpty = new List<object>(0);
         private DataTemplate itemTemplate = null;
@@ -204,45 +255,8 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 9 </since_tizen>
         public override IEnumerable ItemsSource
         {
-            get
-            {
-                return itemsSource;
-            }
-            set
-            {
-                if (itemsSource != null)
-                {
-                    // Clearing old data!
-                    if (itemsSource is INotifyCollectionChanged prevNotifyCollectionChanged)
-                    {
-                        prevNotifyCollectionChanged.CollectionChanged -= CollectionChanged;
-                    }
-                    if (selectedItem != null) selectedItem = null;
-                    selectedItems?.Clear();
-                }
-
-                itemsSource = value;
-                if (value == null)
-                {
-                    InternalItemSource?.Dispose();
-                    InternalItemSource = null;
-                    itemsLayouter?.Clear();
-                    ClearCache();
-                    return;
-                }
-                if (itemsSource is INotifyCollectionChanged newNotifyCollectionChanged)
-                {
-                    newNotifyCollectionChanged.CollectionChanged += CollectionChanged;
-                }
-
-                InternalItemSource?.Dispose();
-                InternalItemSource = ItemsSourceFactory.Create(this);
-
-                if (itemsLayouter == null) return;
-
-                needInitalizeLayouter = true;
-                Init();
-            }
+            get => (IEnumerable)GetValue(ItemsSourceProperty);
+            set => SetValue(ItemsSourceProperty, value);
         }
 
         /// <summary>