简体   繁体   中英

Odd behaviour when replacing DataGridCheckBoxColumn with a DataGridTemplateColumn

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 of DataGridTemplateColumn , I think this is happening because inside the DataGridCheckBoxColumn the CellTemplate and EditingCellTemplate would have been implemented differently. In case of CellTemplate it would be defined as readonly and once you double click you go in editing mode ie EditingCellTemplate and only then you can modify the checkbox 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 in custom template mode is not following the GridReadOnly option, I think this is happening due to the fact that once you define cell templates yourself ie cellEditing template and non-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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM