Context Menu items command binding WPF using MVVM 
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 am not able to understand them and implement in my application. So I thought of putting some code from my application so that you people can help me better.

Problem Statement : I am using a WPF DataGrid. I have added a **Context Menu** and I have 3 options Cut,Copy,Paste. I am using MVVM for development. I want to DataBind these options to Commands in my ViewModel. But I am unable to do the same. The context menu options are not getting data bound at all !!! 

This is my Grid Code in XAML :

> &lt;custom:DataGrid  
      x:Name="DataGrid_Standard"   
       Grid.Row="1"    
        Grid.Column="1"   
       AutoGenerateColumns="False"                                                           
        IsSynchronizedWithCurrentItem="True"   
                     Background="Transparent"
                     ItemsSource="{Binding FullGridData}" 
                     ItemContainerStyle="{StaticResource defaultRowStyle}"
                     ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"                         
                     Grid.ColumnSpan="2"&gt;


Then I have a ContextMenu and a Style for Header Element

>&lt;ContextMenu x:Key="columnHeaderMenu"&gt;<br>
                           &lt;MenuItem Command="{Binding CutCommand}"
                          Header="Test" /&gt;<br>
                        &lt;MenuItem Header="Copy"/&gt;
                        &lt;MenuItem Header="Paste"/&gt;<br>
                    &lt;/ContextMenu&gt;  <br><br>
     &lt;Style TargetType="{x:Type custom:DataGridColumnHeader}" x:Key="DefaultColumnHeaderStyle"&gt;
                        &lt;Setter Property="ContextMenu" Value="{DynamicResource columnHeaderMenu}" /&gt;<br>
                    &lt;/Style&gt;


This line goes in my constructor

      public Window1()
        {            
            this.DataContext = new AppData();
        }


This code goes in my AppData class:



    public class AppData  
    { 

        private IList<GridData> fullGridData = new ObservableCollection<GridData>();<br> 

        public IList<GridData> FullGridData
        {
            get { return fullGridData; }
            set { fullGridData = value; }
        }

        private DelegateCommand<object> cutCommand;
        public DelegateCommand<object> CutCommand
        {
            get
            {
                if (cutCommand == null)
                {
                    cutCommand = new DelegateCommand<object>(CutColumn);
                }
                return cutCommand;
            }
        }

        private void CutColumn(object obj)
        {
            //some code goes here
        }   
     }

  
<br>

** I want to know exactly where am I doing wrong?? Why is the DataBinding not happening??
Please help me regarding this. Please provide me the sample code or modification in my present code from which I can achieve it. **

  




Context Menu items command binding WPF using MVVM 
===

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 am not able to understand them and implement in my application. So I thought of putting some code from my application so that you people can help me better.

Problem Statement : I am using a WPF DataGrid. I have added a **Context Menu** and I have 3 options Cut,Copy,Paste. I am using MVVM for development. I want to DataBind these options to Commands in my ViewModel. But I am unable to do the same. The context menu options are not getting data bound at all !!! 

This is my Grid Code in XAML :

> &lt;custom:DataGrid  
      x:Name="DataGrid_Standard"   
       Grid.Row="1"    
        Grid.Column="1"   
       AutoGenerateColumns="False"                                                           
        IsSynchronizedWithCurrentItem="True"   
                     Background="Transparent"
                     ItemsSource="{Binding FullGridData}" 
                     ItemContainerStyle="{StaticResource defaultRowStyle}"
                     ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"                         
                     Grid.ColumnSpan="2"&gt;


Then I have a ContextMenu and a Style for Header Element

>&lt;ContextMenu x:Key="columnHeaderMenu"&gt;<br>
                           &lt;MenuItem Command="{Binding CutCommand}"
                          Header="Test" /&gt;<br>
                        &lt;MenuItem Header="Copy"/&gt;
                        &lt;MenuItem Header="Paste"/&gt;<br>
                    &lt;/ContextMenu&gt;  <br><br>
     &lt;Style TargetType="{x:Type custom:DataGridColumnHeader}" x:Key="DefaultColumnHeaderStyle"&gt;
                        &lt;Setter Property="ContextMenu" Value="{DynamicResource columnHeaderMenu}" /&gt;<br>
                    &lt;/Style&gt;


This line goes in my constructor

      public Window1()
        {            
            this.DataContext = new AppData();
        }


This code goes in my AppData class:



    public class AppData  
    { 

        private IList<GridData> fullGridData = new ObservableCollection<GridData>();<br> 

        public IList<GridData> FullGridData
        {
            get { return fullGridData; }
            set { fullGridData = value; }
        }

        private DelegateCommand<object> cutCommand;
        public DelegateCommand<object> CutCommand
        {
            get
            {
                if (cutCommand == null)
                {
                    cutCommand = new DelegateCommand<object>(CutColumn);
                }
                return cutCommand;
            }
        }

        private void CutColumn(object obj)
        {
            //some code goes here
        }   
     }

  
<br>

** I want to know exactly where am I doing wrong?? Why is the DataBinding not happening??
Please help me regarding this. Please provide me the sample code or modification in my present code from which I can achieve it. **

  



