如何获取父级的DataContext

问题描述:

iv'e得到一个 ListBox ,该列表框绑定到播放器列表,列表框的父
作为具有播放器属性的数据文本。 p>

iv'e got a ListBox which is bound to a list of Player , the listbox's parent as a datacontext with a property of Players .

   <ListBox>
       <ListBox.Items>
          <Binding Path="Players"></Binding>
       </ListBox.Items>
       <DataTemplate>
          <ListBoxItem>                                        
              <TextBlock  Text="{Binding Name}"></TextBlock>
              <Button ToolTip="Invite To Play" x:Name="btn_InviteToPlay" Click="btn_InviteToPlay_Click>                                         
          </ListBoxItem>
        </DataTemplate>
      </ListBox.ItemTemplate>
   <ListBox>

在按钮上单击事件我需要获取绑定到此
listboxitem(包含点击的按钮的一个)的 Player 的值

on the buttons click event i need to get the value of Player that is bound to this listboxitem (the one containing the button which was clicked )

  private void btn_InviteToPlay_Click(object sender, RoutedEventArgs e)
  {
      Button btn = (Button)sender;
      // how to retrieve player bound to current listboxitem ?   
  }


btn。 DataContext 应该已经包含 Player ,因为 DataContext ListBoxItem

btn.DataContext should already contain the Player as the DataContext is inherited by controls in the ListBoxItem.