Hello, Experts
I have a xamGrid with a checkbox Column like below.
<ig:XamGrid Name="igGridNewLabels">
<ig:TemplateColumn.ItemTemplate>
<DataTemplate>
<CheckBox Name="cbSelected" IsChecked="{Binding Selected, Mode=TwoWay}" Margin="25,0,25,0" Width="20" Click="cbSelected_Click" />
</DataTemplate>
</ig:TemplateColumn.ItemTemplate>
</ig:TemplateColumn>
… …
</ig:XamGrid.Columns>
</ig:XamGrid>
The problem is I want to get the checked rows in this xamGrid. I use following code to get selected rows.
foreach (var row in this.igGridNewLabels.Rows)
{
CheckBox currentCheckbox = (CheckBox)row.Cells[0].Control.Content;
if (currentCheckbox.IsChecked.Value)
// do something.
}
But the problem when there are too many rows in the xamGrid, that cause the scrollbar appears. I fail to get the checkbox for the rows that are in the hidden in the UI. The row.Cells[0].Control throws an nullreferenceException when the foreach loop execute to the hidden area in the UI.
Can you experts help me? This really drive me crazy.
PS: I cannot use DataSource to find the selected rows. Because as you know when the columns are sorted or a filter is applied, the datasource will be inconsistent with UI.
Thanks in advance.
Any suggestion will be much appreciated!!!
Jim
Thank you very much!!
This works!!
Hi Jim,
Because of virtualization, you can't access UI elements of rows that are out of view (they're not yet created). This is why you're getting this Null Ref. exception.
What you could do, is to access the underlying data object for the row, and get the "Selected" value from the data object instead of the checkbox.
Your code should look something like this:
if (((DataObj)row.Data).Selected)
You'll need to replace "DataObj" with the type of your underlying objects.
Hope this helps,