RelativeSource binding not resolving

前端 未结 2 1651
青春惊慌失措
青春惊慌失措 2020-12-22 08:31

I have a ContextMenu in a TreeView

UserControl (DataContext=ViewModel)
    |
    |
    ---- TreeView (ItemSource=MyItems)
           |
           |
                  


        
相关标签:
2条回答
  • 2020-12-22 08:50

    Finally, after many many many google searches I ran into the solution

    <MenuItem
        Header="Exclude season"
        Command="{Binding DataContext.MyRelayCommand, Source={x:Reference _myTreeView}}" />
    

    Because the HierarchicalDataTemplate does not appear in the visual tree, there is not "relative" source...

    I hope this helps somebody else who's pulling his/her hair out...

    0 讨论(0)
  • 2020-12-22 09:06

    Another commonly used solution:

    <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext}" />
    
    0 讨论(0)
提交回复
热议问题