【问题标题】:How would I add Checbox dynamically into a Grid in WPF application如何将复选框动态添加到 WPF 应用程序中的网格中
【发布时间】: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


【解决方案1】:

我建议将这些 CheckBoxes 添加到 StackPanel,然后将 StackPanel 添加到网格中:

StackPanel innerStack;

private void course_Loaded(object sender, RoutedEventArgs e)
{
    innerStack= new StackPanel 
    {
        Orientation=Orientation.Vertical
    };
    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();
        innerStack.Children.Add(cb);
    }
    Grid.SetColumn(innerStack,  /*Set the column of your stackPanel, default is 0*/);
    Grid.SetRow(innerStack,  /*Set the row of your stackPanel, default is 0*/);
    Grid.SetColumnSpan(innerStack,  /*Set the columnSpan of your stackPanel, default is 1*/);
    Grid.SetRowSpan(innerStack,  /*Set the rowSpan of your stackPanel, default is 1*/);

    Grid.Children.Add(innerStack);
}

如果你不想要这种结构,你应该在你的网格中添加一些 RowDefinition 并使用 Grid.SetRow(cb, int) 方法将 ComboBoxes 放在一起。

【讨论】:

    【解决方案2】:

    你做错了。

    先说可以做grid1.Children.Add(cb);

    那么真正的问题是您使用网格来显示列表。在 WPF 中有一个非常好的 ListView,它具有完全可样式化的行,可以包括复选框和几乎所有你能想象到的东西。

    我不知道您的数据是什么样的,所以我无法在 ListView 上进行太多扩展,但类似于

    <ListView ItemsSource="{Binding Courses}">
         <ListView.View>
               <GridView>
                    <GridViewColumn Width="120">
                          <GridViewColumnHeader>
                                <TextBlock Text="Course Name"/>
                          </GridViewColumnHeader>
                          <GridViewColumn.CellTemplate>
                               <DataTemplate>
                                    <TextBlock Text="{Binding ...UFigureThisOut}"/>
                               </DataTemplate>
                          </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="120">
                          <GridViewColumnHeader>
                                <TextBlock Text="Take That"/>
                          </GridViewColumnHeader>
                          <GridViewColumn.CellTemplate>
                               <DataTemplate>
                                    <CheckBox IsChecked="{Binding ...UFigureThisOutToo}"/>
                               </DataTemplate>
                          </GridViewColumn.CellTemplate>
                    </GridViewColumn> 
               </GridView> 
         </ListView.View>
    </ListView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      相关资源
      最近更新 更多