我刚刚遇到了与您相同的问题,并找到了解决方案。
首先,如果您指的是 WPF(而不是 ASP Web 框架),那么您可能指的是 Datagrid 控件而不是 GridView 控件。
这进入 XAML
<DataGrid>
<DataGrid.Columns>
... <!--other definitions of datagrid columns go here-->
<DataGridCheckBoxColumn Header="Select to delete">
<DataGridCheckBoxColumn.ElementStyle>
<Style> <!-- used the following for design sake to center vertically and horizontally the checkboxes relative to the other content in the datagrid-->
<Setter Property="TextBlock.VerticalAlignment" Value="Center" />
<Setter Property="TextBlock.HorizontalAlignment" Value="Center" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
这应该放在后面的 .xaml.cs 代码中
private void getCodMatricol_CheckBox_Selected_in_Datagrid()
{
List<int> your_list_of_items_that_correspond_to_checked_checkboxes = new List<int>();
for (int i = 0; i < datagridGrupeProductie.Items.Count; i++)
{
var item = datagridGrupeProductie.Items[i];
var mycheckbox = datagridGrupeProductie.Columns[10].GetCellContent(item) as CheckBox;
var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;
if ((bool)mycheckbox.IsChecked)
{
your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));
}
}
}
在我的示例中,我提取了数据网格中第一列的内容,其中包含一些由整数表示的代码。
注意:数据网格中的行和列的索引从 [0] 开始(实际上与 C# 中的大多数索引一样)
如果datadgrid中的列定义为(常用)
</DataGridTextColumn> </DataGridTextColumn>
然后它承载一个 TextBlock 元素。
var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;
您必须处理此控件的 Text 属性才能读取其内容并根据需要进行转换。
int.Parse(myTextBlock.Text
接下来,您将使用要提取的内容填充 List 集合:
your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));
此外,如果您想对该值进行任何操作,则必须遍历集合
foreach (var item in your_list_of_items_that_correspond_to_checked_checkboxes)
{
//do whatever needed on each item
}