【发布时间】:2012-12-07 07:57:03
【问题描述】:
我想在我的 C# 桌面应用程序中加载窗口时在 Grid 控件中动态添加一些 Checkbox。复选框将出现多少次取决于表中的条目数。在这里,我使用了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?
}
}
此代码不起作用。有什么建议吗? 谢谢。
【问题讨论】:
-
看看这个也许能解决你的问题:stackoverflow.com/questions/5224181/…
-
您在网格中的绑定看起来如何?
-
如果您要添加多个课程,您也需要添加多个网格行,只需使用 ListView 代替
标签: c# wpf linq-to-sql checkbox grid