How to pass selected row's data to a command?
SfDataGrid provides two events for selection: SelectionChanging and SelectionChanged. It provides the added items and the removed items as arguments in it. You can pass the selected row info to a command in the ViewModel by using the Xamarin.Forms.Behaviors.
Refer the below code example in which a custom behavior for passing the selected row information to ViewModel is defined.
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms" xmlns:local="clr-namespace:SelectionSample" x:Class="SelectionSample.MainPage">
<ContentPage.BindingContext> <local:ViewModel x:Name="viewModel" /> </ContentPage.BindingContext>
<ContentPage.Resources> <ResourceDictionary> <local:CustomConverter x:Key="gridSelectionChangedEventArgs" /> </ResourceDictionary> </ContentPage.Resources>
<syncfusion:SfDataGrid x:Name="dataGrid" ColumnSizer="Star" SelectionMode="Single" ItemsSource="{Binding OrderInfo}"> <syncfusion:SfDataGrid.Behaviors> <local:CustomBehavior Command="{Binding SelectionCommand}" Converter="{StaticResource gridSelectionChangedEventArgs}" /> </syncfusion:SfDataGrid.Behaviors> </syncfusion:SfDataGrid> </ContentPage>s |
The below code illustrates the command and the call back implementation to get the selected row information.
public class ViewModel { private ICommand selectionCommand; public ICommand SelectionCommand { get { return selectionCommand; } set { selectionCommand = value; } } public ViewModel() { selectionCommand = new Command<GridSelectionChangedEventArgs>(OnSelectionChanged); OrderInfoRepository orderInfoRepository = new OrderInfoRepository(); orderInfo = orderInfoRepository.GetOrderDetails(30); } private ObservableCollection<OrderInfo> orderInfo; public ObservableCollection<OrderInfo> OrderInfo { get { return orderInfo; } set { this.orderInfo = value; } } internal void GenerateItemsSource(int count) { OrderInfoRepository order = new OrderInfoRepository(); orderInfo = order.GetOrderDetails(count); } private void OnSelectionChanged(GridSelectionChangedEventArgs e) { var SelectedItem = e.AddedItems; } }
The below code illustrates the behavior class implementation for the SfDataGrid.
public class CustomBehavior : Behavior<SfDataGrid> { public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(CustomBehavior), null); public static readonly BindableProperty InputConverterProperty = BindableProperty.Create("Converter", typeof(IValueConverter), typeof(CustomBehavior), null); public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public IValueConverter Converter { get { return (IValueConverter)GetValue(InputConverterProperty); } set { SetValue(InputConverterProperty, value); } } public SfDataGrid AssociatedObject { get; private set; } protected override void OnAttachedTo(SfDataGrid bindable) { base.OnAttachedTo(bindable); AssociatedObject = bindable; bindable.BindingContextChanged += OnBindingContextChanged; bindable.SelectionChanged += bindable_SelectionChanged; } void bindable_SelectionChanged(object sender, GridSelectionChangedEventArgs e) { if (Command == null) { return; } object parameter = Converter.Convert(e, typeof(object), null, null); var temp = (parameter as GridSelectionChangedEventArgs).AddedItems; if (Command.CanExecute(parameter)) { Command.Execute(parameter); } } protected override void OnDetachingFrom(SfDataGrid bindable) { base.OnDetachingFrom(bindable); bindable.BindingContextChanged -= OnBindingContextChanged; bindable.SelectionChanged -= bindable_SelectionChanged; AssociatedObject = null; } void OnBindingContextChanged(object sender, EventArgs e) { OnBindingContextChanged(); } protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); BindingContext = AssociatedObject.BindingContext; } }
Screenshot:
Sample Link: