From 547ee0114b31bd7dad0b9f337d7b7a08969c9cfb Mon Sep 17 00:00:00 2001 From: SangHyeon Jade Lee Date: Fri, 4 Jun 2021 19:31:22 +0900 Subject: [PATCH] [NUI] Make BindableProperty of ItemsSource (#3160) --- .../Controls/RecyclerView/CollectionView.cs | 92 +++++++++++++--------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs b/src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs index 062aa13..35d639e 100755 --- a/src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs +++ b/src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs @@ -100,6 +100,57 @@ namespace Tizen.NUI.Components return colView.selectionMode; }); + /// + /// Binding Property of items data source. + /// + [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 selectEmpty = new List(0); private DataTemplate itemTemplate = null; @@ -204,45 +255,8 @@ namespace Tizen.NUI.Components /// 9 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); } /// -- 2.7.4