C# wpf中实现多选combobox,即combobox选项为checkbox,怎么通过键盘上下键和回车键操作checkbox的选择?

2024-12-14 16:00:39
推荐回答(1个)
回答1:

protected void Page_Load(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
chk.Text = "testall"; // 这里可以换成数据库的内容
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
chk.AutoPostBack = true;
Page.Form.Controls.Add(chk);

for (int i = 0; i < 10; i++)
{
CheckBox chk2 = new CheckBox();
chk2.Text = "test" + i.ToString(); // 这里可以换成数据库的内容
chk2.Checked = (i % 3 == 0); // 这里可以换成数据库的内容
Page.Form.Controls.Add(chk2);
}

}

void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach(Control ctl in Page.Form.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
chk.Checked = all.Checked;
}
}
}