如何获取父级的DataContext [英] How to get parent's DataContext

查看:133
本文介绍了如何获取父级的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.

这篇关于如何获取父级的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆