1

在我的 WPF 应用程序中,我Checkbox动态显示了一些。Name和这些来自数据库中的表ContentCheckbox

...
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` 事件方法中获取选中/未选中名称的名称。有什么建议吗?谢谢你。

4

1 回答 1

1

相当确定您可以将发件人转换为 CheckBox,这将是您要查找的内容。

于 2012-12-20T11:23:18.217 回答