如何识别在C#WPF应用程序中动态检查哪个动态复选框?

时间:2012-12-20 11:20:37

标签: c# wpf checkbox wpf-controls

在我的WPF应用程序中,我动态显示了一些Checkbox。这些Name的{​​{1}}和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

那么,如何在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? } 名称的名称。任何建议?三江源。

1 个答案:

答案 0 :(得分:1)

相当确定你可以将发送者投射到CheckBox上,这将是你正在寻找的。