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
DataGridCheckBoxColumn
and no click in case ofDataGridTemplateColumn
, I think this is happening because inside theDataGridCheckBoxColumn
theCellTemplate
andEditingCellTemplate
would have been implemented differently. In case ofCellTemplate
it would be defined asreadonly
and once you double click you go in editing mode i.e.EditingCellTemplate
and only then you can modify thecheckbox
and 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
checkbox
incustom template
mode is not following theGridReadOnly
option, I think this is happening due to the fact that once you definecell templates
yourself i.e.cellEditing
template andnon-editing celltemplate
it 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