I have this XAML
<DataGrid Name="grdData" ... >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Something">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chb" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
i try this code to get Checked State
for( int i = 0 ; i < grdData.Items.Count ; i++ )
{
DataGridRow row = ( DataGridRow )grdData.ItemContainerGenerator.ContainerFromIndex( i );
var cellContent = grdData.Columns[ 1 ].GetCellContent( row ) as CheckBox;
if( cellContent != null && cellContent.IsChecked == true )
{
//some code
}
}
my code is wrong?
Since you are looping over the
Itemscollection which is yourItemsSource. Why not to have thebool propertyin your class itself and get it from there itself.Say if ItemSource is
List<Person>, then create a bool property sayIsManagerin classPersonand bind it with your checkBox like this –Now you can loop over the Items to get the value like this –
EDIT
In case you can’t create a property, i would suggest to use the
VisualTreeHelpermethods to find the control. Use this method to find the child (Maybe you can place this in some utility class and use it, since its generic) –Now use the above method to get the state of your checkbox –