Problem with Delete CommandBinding

12 posts, 0 answers
  1. PW
    PW avatar
    13 posts
    Member since:
    May 2009

    Posted 26 May 2010 Link to this post

    We're having problems with a Delete button that we've added as a column in a GridView.  We're using a CommandBinding, but the DeleteExecuted function where we remove the item from the collection doesn't get called, yet the row automagically disappears from the GridView.  I can be debugging for a while trying to delete a bunch of rows (one-at-a-time) without ever hitting a breakpoint set at the very top of the DeleteExecuted function.  During this time the rows are consistently being removed from the GridView - but the DeleteExecuted function isn't called.  It's as if something within the GridView is automagically removing the row and then setting the event as handled and not routing on to the bound command.  Then out of the blue during the same debugging session it will *sometimes* hit my breakpoint, and work reliably from that point on.  Incredibly frustrating. 

    For the record, I'm using the new RadControls for WPF 2010 Q1 release build, running VS 2008 and .NET 3.5.  Updating to VS 2010 and .NET 4 is not an option for us at this time.  I'm about 90% sure that this was working reliably before we upgraded to the 2010 Q1 release, but unfortunately rolling back isn't an attractive option because we needed a fix that became available in the 2010 Q1 release.

    I'm hoping someone out there can throw me some bread crumbs on what might be going on and how to fix it.

    Here's where we define the button for the delete column:
    <telerik:GridViewColumn Header="Delete" IsVisible="True">  
        <telerik:GridViewColumn.CellTemplate> 
            <DataTemplate> 
                <Button Width="18" Height="18" Margin="5,0"   
                        Command="ApplicationCommands.Delete" CommandParameter="{Binding}" > 
                    <Button.Content> 
                        <Grid Width="8.083" Height="8.664" VerticalAlignment="Center" HorizontalAlignment="Center">  
                            <Path Fill="{x:Null}" Stretch="Fill" Stroke="#FF000000" Margin="0.083,0.664,0,0" Data="M0.50001547,0.5 L6.5000797,6.5000169 M6.5000155,0.5 L0.5,6.5000704" StrokeThickness="2"/>  
                            <Path Fill="#FFCE3527" Stretch="Fill" Stroke="#FFCD3527" Margin="0,0,0.083,0.664" Data="M0.50001547,0.5 L6.5000797,6.5000169 M6.5000155,0.5 L0.5,6.5000704" StrokeThickness="2"/>  
                        </Grid> 
                    </Button.Content> 
                </Button> 
            </DataTemplate> 
        </telerik:GridViewColumn.CellTemplate> 
    </telerik:GridViewColumn> 
     

    And the pertinent stuff from the code-behind:

    public ucChartRegionGrid2()  
    {  
        InitializeComponent();  
     
        this.Loaded += new RoutedEventHandler( ucChartRegionGrid2_Loaded );  
        this.CommandBindings.Add( new CommandBinding( ApplicationCommands.Delete,   
                                    this.DeleteExecuted, this.DeleteCanExecute ) );  
        radGridViewDataBinding.SelectionChanged += new   
            EventHandler<Telerik.Windows.Controls.SelectionChangeEventArgs>(radGridViewDataBinding_SelectionChanged);  
    }  
     
    public void DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)  
    {  
        e.CanExecute = true;  
    }  
     
    public void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)  
    {  
        // set a breakpoint here.  It’s not hit, but the row disappears from the grid!?  
        RegionEvent2 deleteRange = e.Parameter as RegionEvent2;  
     
     


    Thanks in advance!
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 27 May 2010 Link to this post

    Hello Darcy Vaughan,

    Your code is not called because RadGridView handles the ApplicationCommands.Delete command internally - it deletes all selected items. We have exposed two events called Deleting and Deleted which can help you react to a Delete request. 

    Hope this helps.


    Best wishes,
    Milan
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. Sivasankar Tanakala
    Sivasankar Tanakala avatar
    1 posts
    Member since:
    Mar 2010

    Posted 24 Aug 2010 Link to this post

    Actually I have similar problem, but not exactly the same. I have a Delete Button (Like a TRASH Icon) as the first column. But when I click on the button the Row of the Button CLick is not getting deleted, whereas the row which is currently selected is getting deleted. How to delete the row on which the Command Button Click happened?
  4. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 25 Aug 2010 Link to this post

    Hello Sivasankar Tanakala,

    You should specify CommandParameter to be able to delete the respective item. For example:

    <telerik:GridViewColumn>
                         <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <telerik:RadButton Content="Delete" Command="telerikGrid:RadGridViewCommands.Delete" CommandParameter="{Binding}" />
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>


    Regards,
    Milan
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. Siva Prakash
    Siva Prakash avatar
    14 posts
    Member since:
    Sep 2011

    Posted 09 Dec 2011 Link to this post

    Hi,

    In my project, I want to invoke the RadGridViewCommands.Delete command in a RadMenuItem Click event.
    As of now, I am doing like this-->

    RadGridViewCommands.Delete.Execute(radgridview1.SelectedItems);

    But this thing not triggering my radgridview's Deleting event in one scenario.

    My scenario is like this :
    • Directly right click on the grid without selecting any row or cell. Then a context menu will open.
    • Click delete menu item in that. Now I am getting the CanExecute value of the Delete command as false here. Thats why deleting event of the radgridview not getting triggered.


    But when you first select a cell and then right click, then the CanExecute is true and deleting event  is getting triggered. So can you please tell me in which scenarios the CanExecute method of the Delete command will return false.


    Regards
    K Siva Prakash.
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 12 Dec 2011 Link to this post

    Hello Siva Prakash,

    Actually, that would be the expected behavior since when you open the context menu, no item is selected and the grid is not aware which item to remove. What you can try is to handle Opening event of RadContextMenu, find the underlying row and set its IsSelected property. It could be something similar to:

    private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadContextMenu menu = sender as RadContextMenu;
                GridViewRow row = menu.GetClickedElement<GridViewRow>();
                if(row != null)
                {
                    row.IsSelected = true;
                }
            }

    Will that approach suit your needs ? 
     

    Kind regards,
    Maya
    the Telerik team

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

  7. Siva Prakash
    Siva Prakash avatar
    14 posts
    Member since:
    Sep 2011

    Posted 13 Dec 2011 Link to this post

    Hi,

    I have already kept the statement of "row.IsSelected=true" in the radcontextmenu openeing event. The row was also getting selected on right clicking on it. But the actual issue I am facing was, the CanExecute() method of the Delete command was returning false, that's why it's not invoking the deleting and delete events of the grid. 
    So can you please suggest me the cases in which this CanExectue() method will return false. 

    Thanks & Regards
    K Siva Prakash.
  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 14 Dec 2011 Link to this post

    Hello Siva Prakash,

    I have tried to reproduce the issue you reported, but I was not able to. Could you take a look at the sample attached and let me know in case I am missing something ? Can you reproduce the same behavior on this application ?
     

    All the best,
    Maya
    the Telerik team

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

  9. bhargava
    bhargava avatar
    14 posts
    Member since:
    Feb 2015

    Posted 17 Feb 2015 Link to this post

    Hi Maya,
    I am having the same problem. This is my code sample
        
    <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu Opening="RadContextMenu_Opening">
                    <telerik:RadMenuItem Header="Add Product"
                                         Command="telerikGrid:RadGridViewCommands.BeginInsert"
                                         CommandTarget="{Binding ElementName=purchaseItemGridView}"
                                         IsEnabled="{Binding CanAddItem, Source={StaticResource purchaseItemsViewModel}}"/>
                    <!--Delete Product is made invisible until the issue related to Enable/Disable is solved. #615789-->
                    <telerik:RadMenuItem Header="Delete Product"
                                         Command="telerikGrid:RadGridViewCommands.Delete"
                                         CommandTarget="{Binding ElementName=purchaseItemGridView}"
                                         IsEnabled="{Binding CanDeleteItem, Source={StaticResource purchaseItemsViewModel}}"/>
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>

    I am changing the visibility of delete in my pitemsVM 
    public bool CanDeleteItem
          {
              get { return _canDeleteItem; }
              set
              {
                  if (_canDeleteItem != value)
                  _canDeleteItem = value;
                 this.OnPropertyChanged(() => CanDeleteItem);
              }
          }

    When I am debugging, it works as expected .but, without attaching a debugger I am not able to see the delete on right click.
    Thanks
    Bhargav
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 18 Feb 2015 Link to this post

    Hello Bhargav,

    Could you clarify what scenario you want to achieve ? When do you want to disable/ enable the command ? 


    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  11. bhargava
    bhargava avatar
    14 posts
    Member since:
    Feb 2015

    Posted 18 Feb 2015 in reply to Maya Link to this post

    Hi Maya,
    I am able to see add and delete product button in context menu when I am debugging and context menu works as expected. When I don't attach any debugger, I only see a add product button enabled, not delete product button even though there are items in the grid.

    I want to see the delete product button enabled without attaching any debugger.

    I have used your above project to implemented  the selected item in opening too.
    when no debugger is attached:

    when debugger is attached:

    I am debugging using VS 2013.

    Thanks,
    Bhargav
  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 18 Feb 2015 Link to this post

    Hello,

    That sounds a bit strange. I updated the project previously attached with the code-snippets you sent. Does this correspond to your scenario  ? When do you want to disable/enable the command ? Could you modify the project so that it illustrates the behavior ? 



    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top