nike
nike

Reputation: 33

Can't Access Checkbox in Datagrid WPF C#

It's my xaml:

<Custom:DataGridTemplateColumn Header="Pilih" Width="50" IsReadOnly="False">
                    <Custom:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Height="23" Name="ckPilih" Checked="ckPilih_Checked">
                            </CheckBox>
                        </DataTemplate>
                    </Custom:DataGridTemplateColumn.CellTemplate>
                </Custom:DataGridTemplateColumn>

when i want to use it(ckPilih) in .cs it can't access

Upvotes: 0

Views: 644

Answers (2)

stukselbax
stukselbax

Reputation: 5935

It is DataTemplate element. you can access it only in your DataTemplate definition. Instead of this, you should use DataGrid.Rows[i].Cell[j].Children property to access collection of controls in a cell.

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

You won't have direct access to this checkbox in code behind because the scope of ckPilih is only inside the DataTemplate

On the side note, I am not sure about your use case but it is not usually recommended to access the checkbox or any other control inside DataTemplate in this manner. You should always try to bind the DataGrid with your datasource. DataGrid will then automatically reflect the changes in DataSource

Upvotes: 1

Related Questions