Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
300
DataPresenterCommand CanExecute in XamContextMenu (of XamDataGrid) not updating
posted

Hi,

We have come across an issue where we have a DataPresenterCommand (copy) in a context menu within a XamDataGrid. The XamDataGrid data is populated via long running worker thread in ViewModel.

The issue we have is we also have a 'Select All' command in the context menu that runs in code behind setting all records to IsSelected = true; When a user clicks Select All, the UI updates but the DataPresenterCommand does not enable. Our users presume that a Ctrl + C shortcut works (as the context menu automatically displays) as the UI is reflecting the selection but the command is still not active - so no selection is actually made.

If a user selects a single row (click on grid) once the grid is populated, then the Command (and ctrl+c shortcut) enable. As such, to select all, user must first click on a single item, then select all (via context menu) then Copy (via context or Ctrl + C). Obviously this behaviour isn't intuitive.

Strangely enough, if I add a separate button which also has the copy command binding, this does enable once the SelectAll method has run however the Ctrl+C shortcut is not available.

I've attached an example application highlighting the issue. Could you please take a look.

XamDataGridNavigation.zip
  • 34430
    Offline posted

    Hello BM,

    Thank you for your post.

    It appears that the reason that this is happening is a focus-related one. Internally, the clipboard-related DataPresenterCommands (Copy, Cut, Paste, etc.) require the XamDataGrid to be focused for their CanExecute method to return true in the case of a context menu. While the XamContextMenu does appear on right click of the grid, this does not appear to focus the grid. My recommendation to prevent this would be to make a call to MyXamDataGrid.Focus() after running your Select-All related code. Alternatively, you could also hook into the XamDataGrid's PreviewMouseRightButtonDown event, and call MyXamDataGrid.Focus() there to focus the grid just before the XamContextMenu shows.

    I have attached a modified version of the sample project you had sent me to demonstrate the above.

    I hope this helps you. Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support

    WpfApplication1.zip