我如何将 Checkbox 动态添加到 WPF 应用程序中的网格中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13961297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:42:13  来源:igfitidea点击:

How would I add Checbox dynamically into a Grid in WPF application

c#wpflinq-to-sqlcheckboxgrid

提问by arefinsami

I would like to add some Checkboxin a Gridcontrol dynamically when the window is loaded in my C# Desktop application. How many times the checkbox will appear depends on the number of entries in a table. Here, I used LINQ To SQLclass. The Grid control is defined in XAML.

当窗口加载到我的 C# 桌面应用程序中时,我想CheckboxGrid控件中动态添加一些。复选框出现的次数取决于表格中的条目数。在这里,我使用了LINQ To SQL类。Grid 控件是在 XAML 中定义的。

...
<Grid Name="grid1">
   <!-- here i would like to show all check box -->
</Grid>
...

Code behind file:

文件隐藏代码:

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?
        }
    }

This code is not working. Any suggession? Thankyou.

此代码不起作用。有什么建议吗?谢谢你。

回答by Sten Petrov

You're doing it wrong.

你这样做是错的。

First to say you can do grid1.Children.Add(cb);

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

Then the real issue is that you're using a grid to display a list. There's a very nice ListView for that in WPF with completely style-able rows that can include checkboxes and pretty much everything else you can imagine.

那么真正的问题是您正在使用网格来显示列表。在 WPF 中有一个非常好的 ListView,它具有完全可设置样式的行,其中可以包含复选框以及您可以想象的几乎所有其他内容。

I don't know what your data looks like so I couldn't expand much on the ListView but something like

我不知道你的数据是什么样的,所以我不能在 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>

回答by Ramin

I suggest adding these CheckBoxes to a StackPanel and then add the StackPanel to the grid:

我建议将这些复选框添加到 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);
}

If you do not want this structure, you should add some RowDefinition to your grid and use Grid.SetRow(cb, int) method to put ComboBoxes over each other.

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