当窗口加载到我的 C# 桌面应用程序中时,我想Checkbox
在控件中动态添加一些内容。Grid
复选框将出现多少次取决于表中的条目数。在这里,我使用LINQ To SQL
了类。Grid 控件在 XAML 中定义。
...
<Grid Name="grid1">
<!-- here i would like to show all check box -->
</Grid>
...
文件后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
// class declaration ...
...
private void course_Loaded(object sender, RoutedEventArgs e)
{
List<Course> courses = ldc.Courses.ToList();
foreach (var c in courses)
{
CheckBox cb = new CheckBox();
cb.Name=c.CourseID.ToString();
cb.Content = c.CourseID.ToString();
//grid1.Controls.Add(cb); does not work. what to do here?
}
}
此代码不起作用。有什么建议吗?谢谢你。