运行界面:
前台代码:
<Grid>
<DataGrid AutoGenerateColumns="True" Height="211" HorizontalAlignment="Left" Margin="27,34,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="334" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="选择">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate />
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<CheckBox Tag="{Binding Path=uid}" Checked="CheckBox_Checked" Name="check1" />
</DataTemplate >
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="388,126,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
后台代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (stu item in dataGrid1.Items)
{
DataGridTemplateColumn templeColumn = dataGrid1.Columns[0] as DataGridTemplateColumn;
FrameworkElement s = dataGrid1.Columns[0].GetCellContent(item);
CheckBox cb = templeColumn.CellTemplate.FindName("check1", s) as CheckBox;
if (cb.IsChecked == true)
MessageBox.Show(Convert.ToString(cb.Tag));
}
}