首页 > 其他 > 详细

如何在后台获取DataGrid中的一个CheckBox

时间:2021-05-27 17:00:40      阅读:7      评论:0      收藏:0      [点我收藏+]

WPF:

技术分享图片

前台代码:

     <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));
    }
}

 

转自博客园:https://www.cnblogs.com/cncs/archive/2012/07/03/2574884.html

如何在后台获取DataGrid中的一个CheckBox

原文:https://www.cnblogs.com/AnYiJianMo/p/14817380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!