[NUI] Pass SelectionChangedEventArgs by default as SelectionChangedCommandParameter.
authorEverLEEst(SangHyeon Lee) <sh10233.lee@samsung.com>
Mon, 19 Dec 2022 12:54:05 +0000 (21:54 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 27 Dec 2022 05:50:29 +0000 (14:50 +0900)
src/Tizen.NUI.Components/Controls/RecyclerView/CollectionView.cs
test/NUITizenGallery/Examples/CollectionViewTest/AnimalGridPage.xaml
test/NUITizenGallery/Examples/CollectionViewTest/AnimalListPage.xaml
test/NUITizenGallery/Examples/CollectionViewTest/Animals.cs

index 95c41dc..d032345 100755 (executable)
@@ -47,8 +47,8 @@ namespace Tizen.NUI.Components
 
                     oldValue = colView.selectedItem;
                     colView.selectedItem = newValue;
-                    var args = new SelectionChangedEventArgs(oldValue, newValue);
 
+                    var args = new SelectionChangedEventArgs(oldValue, newValue);
                     foreach (RecyclerViewItem item in colView.ContentContainer.Children.Where((item) => item is RecyclerViewItem))
                     {
                         if (item.BindingContext == null)
@@ -1234,6 +1234,10 @@ namespace Tizen.NUI.Components
             if (command != null)
             {
                 var commandParameter = colView.SelectionChangedCommandParameter;
+                if (commandParameter == null)
+                {
+                    commandParameter = args;
+                }
 
                 if (command.CanExecute(commandParameter))
                 {
index 3a065f7..f001afe 100755 (executable)
@@ -26,7 +26,8 @@
               ScrollingDirection="Vertical"
               ItemsSource="{Binding Source}"
               HideScrollbar="true"
-              SelectionMode="None">
+              SelectionMode="SingleAlways"
+              SelectionChangedCommand="{Binding SelectedAnimalChangedCommand}">
 
                 <CollectionView.ItemsLayouter>
                       <GridLayouter />
index b8b0160..84187c8 100755 (executable)
@@ -26,7 +26,8 @@
               ScrollingDirection="Vertical"
               ItemsSource="{Binding Source}"
               HideScrollbar="true"
-              SelectionMode="None">
+              SelectionMode="Single"
+              SelectionChangedCommand="{Binding SelectedAnimalChangedCommand}">
 
                 <CollectionView.ItemsLayouter>
                       <LinearLayouter />
index 14e553a..628ec53 100644 (file)
@@ -19,7 +19,9 @@ using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using System.Runtime.CompilerServices;
 using Tizen.NUI;
+using Tizen.NUI.Components;
 using Tizen.NUI.Binding;
 
 public class Animal : INotifyPropertyChanged
@@ -29,7 +31,7 @@ public class Animal : INotifyPropertyChanged
     private string _imageUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "/images/animals/";
 
     public event PropertyChangedEventHandler PropertyChanged;
-    private void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
+    private void OnPropertyChanged([CallerMemberName] string propertyName="") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
 
 
     public Animal(string name, string scientificName)
@@ -44,7 +46,7 @@ public class Animal : INotifyPropertyChanged
         set
         {
             _name = value;
-            OnPropertyChanged("Name");
+            OnPropertyChanged();
         }
     }
 
@@ -54,7 +56,7 @@ public class Animal : INotifyPropertyChanged
         set
         {
             _scientificName = value;
-            OnPropertyChanged("ScientificName");
+            OnPropertyChanged();
         }
     }
 
@@ -70,7 +72,7 @@ public class Animal : INotifyPropertyChanged
 }
 
 
-public class Animals
+public class Animals : INotifyPropertyChanged
 {
     (string Name,string ScientificName)[] namePool = {
     ("Bald Eagle", "Haliaeetus leucocephalus"),
@@ -97,12 +99,39 @@ public class Animals
     ("Tiger", "Panthera tigris"),
     ("Wolf", "Canis lupus"),
     ("Zebra", "Hippotigris"),
-};
+    };
+
+    public event PropertyChangedEventHandler PropertyChanged;
+    private void OnPropertyChanged([CallerMemberName] string propertyName="") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
+
     public ObservableCollection<Animal> Source {get; private set; } = new ObservableCollection<Animal>();
 
     public Animals()
     {
         for (int i = 0; i < namePool.Length; i++)
             Source.Add(new Animal(namePool[i].Name, namePool[i].ScientificName));
+
+        SelectedAnimalChangedCommand = new Command<SelectionChangedEventArgs>((param) =>
+        {
+            if (param == null) return;
+
+            Animal animal = null;
+            // Single Selection Only have 1 or nil object in the list.
+            foreach (object item in param.PreviousSelection)
+            {
+                animal = item as Animal;
+                if (animal == null) break;
+
+                Console.WriteLine($"Previous selected item {animal.Name}");
+            }
+            foreach (object item in param.CurrentSelection)
+            {
+                animal = item as Animal;
+                if (animal == null) break;
+
+                Console.WriteLine($"Current selected item {animal.Name}");
+        }       });
     }
+
+    public Command<SelectionChangedEventArgs> SelectedAnimalChangedCommand { get; }
 }