mercredi 22 août 2018

Get access to CheckBox from DataGrid

So i have this data grid with 4 columns from which 3 are filled from a XML file. One column is a checkbox which i use to make operations based on my first column on the same row. Example: I check the checkbox on row 4, an event triggers and does some memory editing operation with the value of my first column. Now after my program starts, i want to check if these operations are already done and therefore "pre" check those CheckBoxes.

My problem is that I simply can't find a proper way to gain access to the isChecked value of every single checkbox on each row. I have tried the visual tree helper, but that only gives me rows which i can currently see. The CheckBox is also not part of my ItemsSource, so i can't iterate through it, like i can with my other columns.

I would really appreciate some help, thanks in advance.

XAML:

                    <DataGrid x:Name="booksDataGrid" ItemsSource="{Binding}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Index" Binding="{Binding Path=Attribute[Index].Value}"/>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox x:Name="booksDataGridCheckBox" Checked="booksDataGridCheckBox_CheckedChanged" Unchecked="booksDataGridCheckBox_CheckedChanged"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="Author" Binding="{Binding Path=Attribute[Author].Value}"/>
                        <DataGridTextColumn Header="Title" Binding="{Binding Path=Attribute[Title].Value}"/>
                    </DataGrid.Columns>
                </DataGrid>

Code to fill my DataGrid:

XElement xe = XElement.Load("C:/Books.xml");
        booksDataGrid.DataContext = xe.Elements("Books");




Aucun commentaire:

Enregistrer un commentaire