Overriding DeleteCommand

5 posts, 0 answers
  1. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 28 Sep 2011 Link to this post

    Hello Telerik Team,

    I'would like to override de default GridView DeleteCommand. I'm working with MVVM and my ViewModel object already offers a DelegateCommand to remove items from GridView. I've added a column with Delete button bound to this command to allow users remove items. I'd also like users being able to remove items via Del key, so I'm supplying a custom KeyboardCommandProvider like this:
    public class RadGridViewCustomDeleteKeyProvider : DefaultKeyboardCommandProvider
        {
            public RadGridViewCustomDeleteKeyProvider(GridViewDataControl dataControl)
                : base(dataControl)
            {
            }
     
            public ICommand DeleteCommand { get; set; }
     
            public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
            {
                var commands = base.ProvideCommandsForKey(key).ToList();
                if (commands.Contains(RadGridViewCommands.Delete))
                {
                    commands.Remove(RadGridViewCommands.Delete);
                    commands.Add(DeleteCommand);
                }
                return commands;
            }
        }

    The custom command provider works fine. The matter is that when Command execute method is invoked, the argument parameter is null. I expect the parameter being the a row item or a row items collection.

    Is there any way to get the items to delete when pressing "Del" key?
  2. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 28 Sep 2011 Link to this post

    Can anyone help me?

    Thanks!!
  3. DevCraft banner
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Sep 2011 Link to this post

    Hello Daní,

    Actually, the item to be deleted will be the selected one. So, you may just use the SelectedItem property of the grid. 
    However, in case that will not help you in you scenario, I would need a bit more details on your requirements. Why do you need to get the item to be deleted, how will you use it afterwards ?
     

    All the best,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 29 Sep 2011 Link to this post

    Hi Maya,

    I'm tryimg to briefly expose my scenario. Below I'm pasting a simple summary of the classes involved:
    public class ItemViewModel : ViewModelBase
           {
               private bool _canBeDeleted;
     
               private string _myField1;
     
               private string _myField2;
     
               private string _myField3;
     
               public bool CanBeDeleted
               {
                   get { return _canBeDeleted; }
                   set
                   {
                       if (_canBeDeleted != value)
                       {
                           _canBeDeleted = value;
                           OnPropertyChanged("CanBeDeleted");
                       }
                   }
               }
     
               public string MyProperty1
               {
                   get { return _myField1; }
                   set
                   {
                       if (_myField1 != value)
                       {
                           _myField1 = value;
                           OnPropertyChanged("MyProperty1");
                       }
                   }
               }
     
               public string MyProperty2
               {
                   get { return _myField2; }
                   set
                   {
                       if (_myField2 != value)
                       {
                           _myField2 = value;
                           OnPropertyChanged("MyProperty2");
                       }
                   }
               }
     
               public string MyProperty3
               {
                   get { return _myField3; }
                   set
                   {
                       if (_myField3 != value)
                       {
                           _myField3 = value;
                           OnPropertyChanged("MyProperty3");
                       }
                   }
               }
           }
     
           public class ViewModel : ViewModelBase
           {
               public ViewModel()
               {
                   DeleteItemCommand = new DelegateCommand(DeleteItem, CanDeleteItem);
               }
     
               public ObservableCollection<ItemViewModel> Items { get; set; }
     
               public DelegateCommand DeleteItemCommand { get; set; }
     
               private void DeleteItem(object argument)
               {
                   var item = argument as ItemViewModel;
                   Items.Remove(item);
               }
     
               private bool CanDeleteItem(object argument)
               {
                   var item = argument as ItemViewModel;
                   return item != null && item.CanBeDeleted;
               }
           }

    As you can see, I have a ViewModel with an ObservableCollection of ItemViewModel and a DelegateCommand that allow delete items only if the item can be deleted. The custom KeyboardCommandProvider is overriding the default GridView DeleteCommand with the ViewModel.DeleteItemCommand. What I expect is that when GridView executes the command execute method it passes the row item being deleted. Of course, this is an example, I need to perform several tasks whenever an item is deleted. Hope this helps.

    I can add a property a SelectedItem property to the ViewModel to get it synchronized wiht the GridView.SelectedItem (using two way binding). Probably I will need another DelegateCommand, DeleteSelectedItemCommand that will override the default GridView.DelegateCommand. In addition, I'd like to offer the chance to delete multiple items in a row, but I can't see any SelectedItems property in GridView public interface, just SelectedItem. How can I get all the GridView selected items?
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Sep 2011 Link to this post

    Hi Daní,

    Indeed, what I would recommend is is to bind the SelectedItem property to a property in your ViewModel. Thus you will be able to get the item to be deleted. Considering how to get all the selected items, you can use SelectedItems collection of the grid. You can also take a look at this blog post for a reference. 

    Best wishes,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft banner