I know this question has been asked many times in different ways in many websites and also in StackOverFlow but all the answers I found are not helping me ot to be precise I
You have two options. Note: the code samples I added here a similar, but not same as your samples.
Move the definition of the ContextMenu inside the DataGrid defintion as follows:
Or better add a CommandReference to your Resources and set the Command in the MenuItem to a StaticResource as follows: