DataGrid中RowDetailsTemplate内的按钮 - 如何知道单击哪一行?

时间:2011-06-08 22:11:05

标签: c# wpf xaml data-binding datagrid

我有这个XAML

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel>
            <Button Content="Reply" Name="btnReply" Click="btnReply_Click" HorizontalAlignment="Left" Margin="5" Padding="10,0" />
            <Label Content="{Binding Path=Subject}" FontWeight="Bold" />
            <Label Content="{Binding Path=Body}" />
        </StackPanel>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

DataGrid绑定到List 在btnReply_Click里面我想得到MyObject的实例

我该怎么做?

1 个答案:

答案 0 :(得分:1)

var myObject = (sender as FrameworkElement).DataContext as MyObject;

由于DataContext是继承的。