Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value of checkbox from datagrid?

<DataGridTemplateColumn Header="IsAdmin">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="IsAdminCheckBox" IsChecked="{Binding Path=IsAdmin}" Click="IsAdmin_Click" CommandParameter="{Binding Path=Id}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This is the code that I have for a datagrid in my program. In the CS file, in the click event, I want to see if when they check it, it changed to checked, or unchecked, so that I can handle in the database, changing the user from admin to not admin and vise versa. However just calling IsAdminCheckBox doesn't work because it can't find it, and also calling the datagridName.IsAdminCheckBox doesn't work. Does anyone know how to access the status of the checkbox?

like image 501
pongahead Avatar asked Dec 21 '25 22:12

pongahead


1 Answers

You can't access the checkbox this way, because there may be more than one of them, the name is valid only inside the DataTemplate.

In the event handler, the sender parameter should be the CheckBox, you just have to cast it and then you can access the IsChecked property. Another option would be to handle the Checked and Uncheked events.

But I think a better way would be to handle this in the bound class, not in code-behind of your GUI, separation of concerns and all that.

like image 55
svick Avatar answered Dec 23 '25 11:12

svick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!