This is a migrated thread and some comments may be shown as answers.

GrdMultipleBins_SelectionChanged Requires two clicks

3 Answers 50 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 18 Sep 2019, 03:20 PM

For some reason my grid is requring to clicks for the user to performe the click action the first click to select the row they are on then another click to perfome the action i have coded behind the function I have the functions under the 

 

But to the end user this feels like they are double clicking as have to select the row first is their any way to get both events to fire same time so that it selects the row and then fires selection changed right away ?

     private async void GrdMultipleBins_SelectionChanged(object sender, Telerik.XamarinForms.DataGrid.DataGridSelectionChangedEventArgs e)
     {

     }

3 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 18 Sep 2019, 11:16 PM

Hello David,

That is the expected behavior for a SelectionChanged event. What I think you might be looking for instead is an "ItemTapped" type of event, like the RadListView has.

The RadDataGrid doesn't have a Tapped event yet, but you can still achieve the same result by using the CellTap command. Although the name is CellTap, you get the row's data item in the parameter.

Take a look at the DataGrid Commands documentation for an explanation on how to use commands, then visit the CellTap walk-through article for complete demo code. You can also visit the SDKExamples DataGrid Commands source code.

Circling back to the data item, you can get a reference to the data item for that row via the parameter. The parameter is a DataGridCellInfo object and the DataGridCellInfo.Item property is the row's data.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 19 Sep 2019, 08:31 AM
I kinda need this to happen with in the call of the form cause I present the user with a  quantity field in a popup which is then displayed and refered to within the form its not ideal that i have to switch to another class can the list view be made to look like the grid view with minaml effort?.
0
Lance | Manager Technical Support
Telerik team
answered on 19 Sep 2019, 03:18 PM

Hello David,

I'm not sure what you mean by "within the call of the form". I'm guessing you're asking if you can run the Execute method in that page's code-behind like an event handler? Unfortunately, this is not how MVVM is set up to work as it's specifically designed to be decoupled from the page object.

What would typically be done is there would be a string property in the view model that you can Bind to the Popup's content. Then when the command is executed, that string property would get updated with the tapped row's details.

Customizing The Command

However, I can share an example of how you can make the command work like a DelegateCommand. After this, you can define the command anywhere you want (and eventually define the delegate's action in the page code-behind).

As with most Command objects, you can finagle it to do what you want by adding whatever custom things you need. In this case, since you want it to act like a DelegateCommand, you can add an Action field to the command class. Then, invoke your custom action when the DataGridCommand is executed.

Example

Here's one example you can use to take that route. Please note that this isn't production-ready code, it's meant as an example for you to see how it can be done. You will want to add some null checks and other protections in place before using it .

Take a look at the updated CellTapUserCommand below. It's the same class from our demos, but I added an Action field to the class and that Action is invoked when the DataGridCommand's Execute method is invoked.

public class CellTapUserCommand : DataGridCommand
{
    // 1. Define a custom Action field so you can define an external Action
    private readonly Action<DataGridCellInfo> externalExecute;

    public CellTapUserCommand(Action<DataGridCellInfo> execute)
    {
        Id = DataGridCommandId.CellTap;

        // 2. Set the Action when the class is instantiated
        externalExecute = execute;
    }

    public override bool CanExecute(object parameter)
    {
        return true;
    }
    public override void Execute(object parameter)
    {
        // 3. Invoke the external Action instead of running the logic in here.
        externalExecute.Invoke(parameter as DataGridCellInfo);

        this.Owner.CommandService.ExecuteDefaultCommand(DataGridCommandId.CellTap, parameter);
    }
}

With the class now supporting external Actions, you can use it in your code behind. Let's continue with the official Command example I shared. Notice the constructor difference:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        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;

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

    // Your custom action delegate, acting like and event handler
    private void TapExecuted(DataGridCellInfo cellInfo)
    {
        if (cellInfo.Item is Country country)
        {
            Debug.WriteLine($"You tapped {country.Name}");
        }
    }
}

Now that the method is defined in the page's code behind, you can use it like an event handler method.

Demo

I've attached a demo that will open a popup, with that row's info, when a row is tapped. Here's the result at runtime:

If you have any further trouble, please open a Support ticket using the account that you have a license with and we'll dig deeper.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DataGrid
Asked by
David
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
David
Top achievements
Rank 1
Share this question
or