public class ViewModel {
public ViewModel()
{
gridTappedCommand = new Command(SfGrid_GridTapped);
SetRowstoGenerate(50);
}
private ObservableCollection<OrderInfo> ordersInfo;
public ObservableCollection<OrderInfo> OrdersInfo
{
get { return ordersInfo; }
set { this.ordersInfo = value; }
}
private Command gridTappedCommand;
public Command GridTappedCommand
{
get { return gridTappedCommand; }
set { gridTappedCommand = value; }
}
public void SetRowstoGenerate(int count)
{
OrderInfoRepository order = new OrderInfoRepository();
ordersInfo = order.GetOrderDetails(count);
}
private void SfGrid_GridTapped()
{
}
} |
Approach - 2: Writing behavior for SfDataGrid
Please refer the below code example in which we have written Behavior for SfDataGrid to implement the GridTapped event of SfDataGrid in the ViewModel.
The below code illustrates implementing the GridTapped event in ViewModel by writing Behavior for SfDataGrid.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="DataGridDemo.DataGrid"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
xmlns:local="clr-namespace:DataGridDemo"
xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms">
<ContentPage.BindingContext>
<local:ViewModel x:Name="viewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<sfgrid:SfDataGrid x:Name="dataGrid"
AutoGenerateColumns="False"
ColumnSizer="Star"
ItemsSource="{Binding OrdersInfo}">
<sfgrid:SfDataGrid.Behaviors>
<local:SfDataGridBehavior Parameter="{x:Reference Name=viewModel}" />
</sfgrid:SfDataGrid.Behaviors>
</sfgrid:SfDataGrid>
</ContentPage.Content>
</ContentPage> |
The below code illustrates writing behavior for SfDataGrid to implement the GridTapped event in ViewModel.
//Behavior class implementation
public class SfDataGridBehavior : Behavior<SfDataGrid>
{
public static readonly BindableProperty ParameterProperty =
BindableProperty.Create("Parameter", typeof(ViewModel), typeof(SfDataGridBehavior), null);
public ViewModel Parameter
{
get { return (ViewModel)GetValue(ParameterProperty); }
set { SetValue(ParameterProperty, value); }
}
public SfDataGrid AssociatedObject { get; private set; }
protected override void OnAttachedTo(SfDataGrid bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
bindable.GridTapped += Parameter.SfGrid_GridTapped;
}
protected override void OnDetachingFrom(SfDataGrid bindable)
{
base.OnDetachingFrom(bindable);
bindable.GridTapped -= Parameter.SfGrid_GridTapped;
AssociatedObject = null;
}
}
//ViewModel implementation
public class ViewModel
{
public ViewModel()
{
SetRowstoGenerate(50);
}
private ObservableCollection<OrderInfo> ordersInfo;
public ObservableCollection<OrderInfo> OrdersInfo
{
get { return ordersInfo; }
set { this.ordersInfo = value; }
}
public void SetRowstoGenerate(int count)
{
OrderInfoRepository order = new OrderInfoRepository();
ordersInfo = order.GetOrderDetails(count);
}
internal void SfGrid_GridTapped(object sender, GridTappedEventsArgs e)
{
int rowindex = e.RowColumnindex.RowIndex;
int columnindex = e.RowColumnindex.ColumnIndex;
}
} |
We have prepared a sample to adapt both the approaches for your reference and you can download the same from the below link.
Sample link: http://www.syncfusion.com/downloads/support/forum/128250/ze/DataGridDemo745397101
Regards,
Harikrishnan