New to Telerik UI for Xamarin? Download free 30-day trial

CellTap Command

The DataGrid CellTap Command handles the Tap gesture over a grid cell, that is, the intersection of a data row and a column.

Example

Here is an example how the RadDataGrid CellTap Command works:

First, create the needed business objects, for example type Country with the following properties:

public class Country
{
    public Country(string name, double population)
    {
        this.Name = name;
        this.Population = population;
    }

    public string Name { get; set; }
    public double Population { get; set; }
}

Here is the simple data used as binding context:

var source = new ObservableCollection<Country>();
source.Add(new Country("Mozambique", 24692000));
source.Add(new Country("Paraguay", 6725000));
source.Add(new Country("Turkmenistan", 5663000));
source.Add(new Country("Mongolia", 3027000));
source.Add(new Country("Japan", 127000000));
source.Add(new Country("Bulgaria", 7128000));
source.Add(new Country("Chad", 14450000));
source.Add(new Country("Netherlands", 17020000));

this.BindingContext = source;

Then handle the CellTap action as a Command. First, create a class that inherits from the DataGridCommand and set its Id property accordingly. You would also need to override CanExecute and Execute methods as demostrated in the example below:

public class CellTapUserCommand : DataGridCommand
{
    public CellTapUserCommand()
    {
        Id = DataGridCommandId.CellTap;
    }
    public override bool CanExecute(object parameter)
    {
        return true;
    }
    public override void Execute(object parameter)
    {
        var context = parameter as DataGridCellInfo;
        var cellTap =  $"You tapped on {context.Value} inside {context.Column.HeaderText} column \n";
        Application.Current.MainPage.DisplayAlert("CellTap Command: ", cellTap, "OK");
        this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.CellTap, parameter);
    }
}

Then add this Command to the Commands collection of the RadDataGrid instance:

grid.Commands.Add(new CellTapUserCommand());

Define the RadDataGrid in XAML:

<grid:RadDataGrid x:Name="grid" ItemsSource="{Binding}"/>

SDK Samples Browser application contains an example that shows how to use the CellTap Command. The Commands example is located in the DataGrid/Commands folder.

See Also

In this article