如何使用 C# 在 WPF DataGrid(从数据上下文绑定源)中读取选中的行单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27416336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can read checked row cell value in WPF DataGrid (Bind source from data context) using C#
提问by mukesh_mourya
Here my DataGrid in wpfwin.xaml. I want to collect "Challan_No" value in list but i can't do this ..please help me
这是我在 wpfwin.xaml 中的 DataGrid。我想在列表中收集“Challan_No”值,但我不能这样做..请帮助我
<DataGridTextColumn Header="Chalaan ID" Width="Auto" Binding="{Binding Id, Mode=OneWay}"/>
<DataGridTextColumn Header="Challan No" Width="Auto" Binding="{Binding Challan_No, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Company" Width="Auto" Binding="{Binding Organization_Name, Mode=OneWay}"/>
<DataGridTextColumn Header="Client" Width="Auto" Binding="{Binding Organization, Mode=OneWay}"/>
<DataGridTextColumn Header="Date" Width="Auto" Binding="{Binding Date, Mode=OneWay}"/>
<DataGridTemplateColumn Header="select" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="ckselect" ></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And here, I tried some code to get it in .cs page
在这里,我尝试了一些代码将其放入 .cs 页面
ArrayList list = new ArrayList();
for (int i = 0; i < myGrid.Items.Count; i++)
{
CheckBox mycheckbox = myGrid.Columns[5].GetCellContent(myGrid.Items[i]) as CheckBox;
if (mycheckbox.IsChecked == true)
{
int inde = this.myGrid.SelectedIndex;
DataRowView drv = (DataRowView)myGrid.Items[inde];
object ch = drv[1];
list.Add(ch);
}
}
回答by ELH
first you could simply use a DataGridCheckBoxColumninstead of adding a CheckBoxinside a DataGridTemplateColumn:
首先,您可以简单地使用 aDataGridCheckBoxColumn而不是CheckBox在 a中添加a DataGridTemplateColumn:
<DataGridTextColumn Header="Chalaan ID" Width="Auto" Binding="{Binding Id, Mode=OneWay}"/>
<DataGridTextColumn Header="Challan No" Width="Auto" Binding="{Binding Challan_No, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Company" Width="Auto" Binding="{Binding Organization_Name, Mode=OneWay}"/>
<DataGridTextColumn Header="Client" Width="Auto" Binding="{Binding Organization, Mode=OneWay}"/>
<DataGridTextColumn Header="Date" Width="Auto" Binding="{Binding Date, Mode=OneWay}"/>
<DataGridCheckBoxColumn Header="select" Width="100" />
</DataGrid.Columns>
</DataGrid>
and to get the list of checked items:
并获取已检查项目的列表:
var SelectedList=new List<YourDataGridItemType>();
for (int i = 0; i < MyDataGrid.Items.Count; i++)
{
var item = MyDataGrid.Items[i];
var mycheckbox = MyDataGrid.Columns[1].GetCellContent(item) as CheckBox;
if ((bool)mycheckbox.IsChecked)
{
SelectedList.Add(YourDataGridItemsList[i]);
}
}
where YourDataGridItemsList represent the list of objects that your DataGrid is Binded to .
其中 YourDataGridItemsList 表示您的 DataGrid 绑定到的对象列表。
回答by Phoenix
I think it would be best if you added an IsSelected property to whatever type it is you are displaying and then just TwoWay bind that property to the checkbox column. That way you can simply do this to get the selected elements:
我认为最好是将 IsSelected 属性添加到您显示的任何类型,然后只需 TwoWay 将该属性绑定到复选框列。这样您就可以简单地执行此操作来获取所选元素:
mycollection.Where(x=>x.IsSelected);

