C# ListView - 控制 checkBox 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/976569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:41:23  来源:igfitidea点击:

C# ListView - control checkBox event

c#listviewcontrols

提问by user366312

Suppose I need to change the status of an item from active = true to active = false and vise versa and at the same time persist my change in the database table.

假设我需要将一个项目的状态从 active = true 更改为 active = false ,反之亦然,同时在数据库表中保留我的更改。

I tested ItemChecked event like the following:

我测试了 ItemChecked 事件,如下所示:

    private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        ListViewItem item = (ListViewItem)sender;

        if (item != null)
        {
            Book b = (Book) item.Tag;

            b.MakeActive(item.Checked);
        }
    }

I failed.

我失败了。

Can anyone help me?

谁能帮我?

采纳答案by Stan R.

in this case object senderis ListViewand not ListViewItemyour code should be this

在这种情况下object senderListView而不是ListViewItem你的代码应该是这样的

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
   ListViewItem item = e.Item as ListViewItem;

    if (item != null)
    {
        Book b = (Book) item.Tag;

        b.MakeActive(item.Checked);
    }
}