WPF Context menu doesn't bind to right databound item

前端 未结 7 1287
青春惊慌失措
青春惊慌失措 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 12:05

    I prefer another solution. Add context menu loader event.

    <ContextMenu Loaded="ContextMenu_Loaded"> 
        <MenuItem Header="Change" Command="{Binding Path=ChangeCommand}"/> 
    </ContextMenu> 
    

    Assign data context within the event.

    private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
    {
        (sender as ContextMenu).DataContext = this; //assignment can be replaced with desired data context
    }
    
    0 讨论(0)
提交回复
热议问题