GrdMultipleBins_SelectionChanged Requires two clicks

4 posts, 0 answers
  1. David
    David avatar
    77 posts
    Member since:
    Nov 2011

    Posted 18 Sep 2019 Link to this post

    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)
         {

         }

  2. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1195 posts

    Posted 18 Sep 2019 Link to this post

    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
  3. David
    David avatar
    77 posts
    Member since:
    Nov 2011

    Posted 19 Sep 2019 in reply to Lance | Manager Technical Support Link to this post

    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?.
  4. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1195 posts

    Posted 19 Sep 2019 Link to this post

    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
Back to Top