ContextMenu with Command in DataGrid: Null CurrentItem in CanExecute - bug?

Dec 7, 2008 at 11:00 AM
Edited Dec 7, 2008 at 9:15 PM
I have a ContextMenu in DataGrid with some MenuItems attached to a command. When CanExecute for any of these commands is fired, the CurrentItem of the DataGrid is set to Null though it reality it is not because I fire the ContextMenu after selecting a valid cell in the grid. Here is the markup

<MenuItem Command="local:Commands.ShowDealDetails"
  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},    Path=PlacementTarget.CurrentItem}"/>

Dec 11, 2008 at 4:00 PM
Can anybody please confirm this or not? Did anybody try a similar setup and it worked?
Dec 12, 2008 at 10:10 PM
I'm having the same issue, however, it appears not null at times as well.  I am also clicking a valid cell in the grid before I do the right-click to use the context menu, though it should not matter.  I have yet to try and dig in and find out exactly why.  As soon as I do I'll post again.

Dec 15, 2008 at 9:17 AM
I found that I had to add an event handler for MouseRightButtonUp for a cell to be selected on right click (this happens before the context menu opens). Anything will do, even an empty one. Could this be the same issue?
Dec 18, 2008 at 8:02 PM
If there is any kind of keyboard focus change, the CurrentCell will be updated which will also update the CurrentItem and can possibly make it null.  If you are doing full row selection, binding to PlacementTarget.SelectedItem should work better.  The hack that mikj is doing is allowing the CurrentCell and CurrentItem to be set during that right-click which makes it work.
Dec 29, 2008 at 6:13 PM
to me this looks something like an (inherent) issue (feature/error) with WPF on how it deals with message processing. And somewhere in that logic bound elements like CurrentItem reset whenever there is some change in focus. This and contextmenu not being in the tree, make it almost impossible to make a clean and simple design where it should be like. There is hardly anything more common in UI as context menu.

I have to say it is a very annoying feature.