WPF Context menu doesn't bind to right databound item

前端 未结 7 1285
青春惊慌失措
青春惊慌失措 2020-12-08 11:03

I have a problem when binding a command in a context menu on a usercontrol that is on a tab page. The first time I use the menu (right-click on the tab) it works great, but

7条回答
  •  时光说笑
    2020-12-08 11:58

    The cleanest way I have found to bind commands to context menu items involves using a class called CommandReference. You can find it in the MVVM toolkit on Codeplex at WPF Futures.

    The XAML might look like this:

    
                    
    
                    
                        
                            
                            
                        
                   
        
    

    MyCustomCommand is a RelayCommand on the ViewModel. In this example, the ViewModel was attached to the view's datacontext in the code-behind.

    Note: this XAML was copied from a working project and simplified for illustration. There may be typos or other minor errors.

提交回复
热议问题