How to bind MenuItem.Header to Window/UserControl dependency property?

后端 未结 2 499
礼貌的吻别
礼貌的吻别 2021-01-06 09:30

I`m wondering how can I bind MenuItem.Header to the parent Window/UserControl dependency property? Here is a simple example:

Window1.xaml:



        
2条回答
  •  走了就别回头了
    2021-01-06 10:29

    You should see this in the Output window of Visual Studio:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=self'. BindingExpression:Path=MenuText; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Header' (type 'Object')

    That is because the ContextMenu is disconnected from the VisualTree, you need to do this Binding differently.

    One way is via ContextMenu.PlacementTarget (which should be the Grid), you could use its DataContext to establish a binding, e.g.:

    
    

    or set up the DataContext in the ContextMenu itself:

    
        
    
    

    If this is not an option (because the DataContext of the Grid cannot be the Window/UserControl) you can try to pass the reference to the Window/UserControl through the Tag of your Grid for example.

    
        
            
            
                
            
        ...
    

    As a side-note: Because of this behavior i tend to define a helper-style in App.xaml to make all ContextMenus "pseudo-inherit" the DataContext from their parent:

        
        
    

提交回复
热议问题