简体   繁体   English

从数据网格中获取复选框的值?

[英]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.在 CS 文件中,在单击事件中,我想看看当他们检查它时,它是否更改为选中或未选中,以便我可以在数据库中处理,将用户从管理员更改为非管理员,反之亦然。 However just calling IsAdminCheckBox doesn't work because it can't find it, and also calling the datagridName.IsAdminCheckBox doesn't work.但是,仅调用 IsAdminCheckBox 不起作用,因为它找不到它,并且调用 datagridName.IsAdminCheckBox 也不起作用。 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 .您不能以这种方式访问复选框,因为其中可能有多个,名称仅在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.在事件处理程序中, sender参数应该是CheckBox ,您只需对其进行强制转换,然后您就可以访问IsChecked属性。 Another option would be to handle the Checked and Uncheked events.另一种选择是处理CheckedUncheked事件。

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.但我认为更好的方法是在绑定的 class 中处理这个问题,而不是在你的 GUI 的代码隐藏中,关注点分离等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM