在我的 WPF 应用程序中,我Checkbox
动态显示了一些。Name
和这些来自数据库中的表Content
。Checkbox
...
StackPanel innerStack = new StackPanel();
List<Course> courses = ldc.Courses.ToList();
var count = courses.Count();
var b = ldc.Books.Single(x=>x.BookID==1);
foreach (var c in courses)
{
CheckBox cb = new CheckBox();
cb.Name = c.CourseID.ToString();
cb.Content = c.CourseID.ToString();
var x = from bc in ldc.CourseAndBooks
where bc.BookID == b.BookID
select bc.CourseID;
if (x.Contains(c.CourseID))
{
cb.IsChecked = true;
}
cb.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(course_Checked));
cb.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(course_Unchecked));
innerStack.Children.Add(cb);
}
Grid.SetColumn(innerStack,0);
Grid.SetRow(innerStack,0);
Grid.SetColumnSpan(innerStack,1);
Grid.SetRowSpan(innerStack,1);
grid.Children.Add(innerStack);
...
我也添加了Checked
事件Unchecked
来操作数据库中的选中项目。
private void course_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("checked"); // it works
// How to identify cb.Name here to perform some database query?
}
private void course_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("Unchecked"); // it works
// How to identify cb.Name here to perform some database query?
}
那么,如何Checkbox
在 Checked/Unchecked` 事件方法中获取选中/未选中名称的名称。有什么建议吗?谢谢你。