码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-06-02 12:57:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:style   代码   nta   tag   mes   log   ring   checked   前台   

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

标签:style   代码   nta   tag   mes   log   ring   checked   前台   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!