我的WPF应用程序中有一个DataGrid ,如下所示。
<DataGrid Name="stDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Edit" CanUserResize="False" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button" Click="btnEdit_Click">
<StackPanel>
<Image Source="images/edit.png"/>
</StackPanel>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Age}" Header="Age" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=Sex}" Header="Sex" IsReadOnly="True"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
像这样将数据绑定到 DataGrid 中。
using (var context = new CLASS_DBEntities())
{
var query = from s in context.STUDENT
orderby s.STUDENT_NAME
select new {s.STUDENT_ID, Name = s.STUDENT_NAME, Age = s.STUDENT_AGE,
Sex = s.STUDENT_SEX};
stDataGrid.ItemsSource = query.ToList();
}
当用户单击 DataGrid 中的按钮时,我需要获取STUDENT_ID
值。我怎样才能做到这一点 ?