MVVM Commanding With Grid Row Selection

Is there a way to not have to Type the command with "SFDataGrid"? Can we just pass in the collection of selected items so the viewmodel doesn't know about the grid itself?

        public Command GridRowSelectionCommand
        {
            get
            {
                return this.gridRowSelectionCommand ?? (this.gridRowSelectionCommand = new Command<SfDataGrid>((e) =>
                {
                    try
                    {
                        if (e != null)
                        {
                        }
                    }
                    catch (Exception x) { x.ToString(); }
                }));
            }
        }


5 Replies

JA Jayaraman Ayyanar Syncfusion Team March 1, 2018 06:56 PM UTC

Hi  Todd, 
  
We have checked your query. The requirement can be achieved by maintain the separate collection for the selected item in the datagrid using the collection changed event.    
 
The below code example illustrates creating collection for selected items. 
 
public class ViewModel : NotificationObject 
    { 
       public ViewModel() 
        { 
           gridSelectedItems = new ObservableCollection<object>(); 
           gridSelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged; 
        } 
        private void GridSelectedItems_CollectionChanged(objectsender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
        { 
        } 
 
        private ObservableCollection<object> gridSelectedItems; 
 
        public ObservableCollection<object> GridSelectedItems 
        { 
            get { return gridSelectedItems; } 
            set { this.gridSelectedItems = value; } 
        } 
} 
 
The below code example illustrates binding of the collection in selected items : 
 
  <sfgrid:SfDataGrid x:Name="dataGrid"  AllowEditing="True" Padding="20" 
                       ItemsSource="{Binding OrdersInfo,Mode=TwoWay}" SelectionMode="Multiple" 
                       AutoGenerateColumns="False" 
                           AllowDraggingRow="True" SelectedItems="{Binding GridSelectedItems,Mode=TwoWay}" 
                           AllowDraggingColumn="True" 
                       ColumnSizer="None" AllowGroupExpandCollapse="True" ShowColumnWhenGrouped="True"> 
</sfgrid:SfDataGrid> 
 
 
Let us know if it is not fulfill your requirement please update us with more details. 
 
Regards, 
Jayaraman 



TD Todd Daniels March 1, 2018 07:48 PM UTC

Perfect, thank you.


JA Jayaraman Ayyanar Syncfusion Team March 2, 2018 04:12 AM UTC

Hi Todd, 
  
Thanks for the update. Please let us know if you require any further assistance. 
   
Regards, 
Jayaraman 



TD Todd Daniels March 2, 2018 04:53 PM UTC

Just a follow up...Can something similar be done for the SFListView?


MK Muthu Kumaran Gnanavinayagam Syncfusion Team March 5, 2018 12:28 PM UTC

Hi Todd, 
 
You can achieve the reported requirement using SfListView which is similar to that of SfDataGrid. We would like to let you know that you can bind a property in ViewModel to SelectedItems property in XAML page to achieve your requirement. The SelectionChanged event in SfListView will be triggered only while performing Selection operation manually by tapping over an item. As the SelectedItems property is of type ObservableCollection, you can trigger the SelectedItems.CollectionChanged event to update the property in ViewModel. 
 
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know If your require further assistance. 

Regards,
 
G.Muthu kumaran. 


Loader.
Up arrow icon