Context Menu items command binding WPF using MVVM

后端 未结 5 511
眼角桃花
眼角桃花 2021-01-01 04:56

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

5条回答
  •  被撕碎了的回忆
    2021-01-01 05:31

    I had the same issue. The command bindings stopped working once I moved them to the ViewModel from code behind. And in the viewmodel I had to change my ICommand from RoutedCommand to DelegateCommand. I was able to get it working in the following way -

    Add Opened eventhandler to your context menu -

    
        
        
        
    
    

    In the code behind, you would assign your ViewModel to the context menu's DataContext -

    private void ContextMenu_Opened(object sender, RoutedEventArgs e)
    {
        ContextMenu menu = sender as ContextMenu;
        menu.DataContext = _vm;
    }
    

提交回复
热议问题