简体   繁体   中英

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?

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.

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