I have discovered an odd behaviour when replacing a DataGridCheckBoxColumn against a DataGridTemplateColumn that contains a Checkbox.
<sdk:DataGrid Grid.Column="0" IsReadOnly="{Binding IsInReadOnlyMode}">
<sdk:DataGrid.Columns>
<sdk:DataGridCheckBoxColumn Header="Sales" Binding="{Binding Path=Sales}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
When the DataGrid is readonly then the checkbox is also disabled. The code above works correct.
Now if I want to achieve the same thing by using DataGridTemplateColumn, the checkbox doesn’t seem to disable itself when the DataGrid is in ReadOnly mode.
<sdk:DataGrid Grid.Column="0" IsReadOnly="{Binding IsInReadOnlyMode}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Sales" >
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<CheckBox>
<CheckBox.IsChecked>
<Binding Path="Sales" Mode="TwoWay"/>
</CheckBox.IsChecked>
</CheckBox>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox>
<CheckBox.IsChecked>
<Binding Path="Sales" Mode="TwoWay"/>
</CheckBox.IsChecked>
</CheckBox>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
One other thing I noticed that might help to explain this is that the checkbox inside DataGridCheckBoxColumn is always disabled until you actually doubleclick the containing cell. Only then once in edit mode I can access the Checkbox.
In the CheckBox I created myself inside DataGridTemplateColumn, the checkbox seem always to be active and can be ticked on and off without even doubleclicking the cell first.
Btw I am using it in Silverlight4, but I am pretty sure it must be the same in WPF.
Can somebody explain to me why that is please?
Thanks,
Regarding the double click in case of
DataGridCheckBoxColumnand no click in case ofDataGridTemplateColumn, I think this is happening because inside theDataGridCheckBoxColumntheCellTemplateandEditingCellTemplatewould have been implemented differently. In case ofCellTemplateit would be defined asreadonlyand once you double click you go in editing mode i.e.EditingCellTemplateand only then you can modify thecheckboxand it makes sense.Now in your case as you have defined both the editing and non-editing template same, so the checkbox is always ready to accept the input
Regarding your main question that why the
checkboxincustom templatemode is not following theGridReadOnlyoption, I think this is happening due to the fact that once you definecell templatesyourself i.e.cellEditingtemplate andnon-editing celltemplateit becomes your responsibility to handle the read only behavior of cells. The option applied on the grid such as read only won’t have any effect in templatedcolumn case