<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 cha开发者_开发问答nged 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.
精彩评论