I'm trying to create a GroupBox, add a Grid (or StackPanel) to it then put some TextBlocks on it, all during runtime. This is what i've tried
我正在尝试创建一个GroupBox,向它添加一个Grid(或StackPanel),然后在运行时添加一些TextBlocks。这就是我尝试过的
GroupBox groupBox1 = new GroupBox();
Grid grid1 = new Grid();
groupBox1.Width = 85;
groupBox1.Height = 60;
grid1.Height = 85;
grid1.Width = 60;
groupBox1.Content = grid1.Children.Add(textBlock1);
groupBox1.Margin = new Thickness(50, 50, 0, 0);
mainWindow.canvas.Children.Add(groupBox1);
But all I get is a groupbox with a thick white border with nothing in it.
但我得到的只是一个带有厚白色边框的组合框,里面没有任何东西。
2 个解决方案
#1
5
As far as I can see a Grid.Children.Add returns an int
and that's not what you want to set the content of the groupBox1
to.
据我所知,Grid.Children.Add返回一个int,而不是你想要将groupBox1的内容设置为。
An untested idea from me as a non WPF expert is to set the grid as the Content of your groupbox.
我作为非WPF专家的一个未经测试的想法是将网格设置为您的组框的内容。
grid1.Children.Add(textBlock1);
groupBox1.Content = grid1;
#2
0
For simple checkboxes i used this code :
对于简单的复选框,我使用此代码:
var container = new FlowLayoutPanel
{
FlowDirection = FlowDirection.TopDown,
Dock = DockStyle.Fill
};
myGroupBox.Controls.Add(container);
foreach (var myText in textList)
{
var checkBox = new CheckBox
{
Text = myText
};
container.Controls.Add(checkBox);
}
Of course the foreach statement is just for the example :)
当然foreach语句仅用于示例:)
#1
5
As far as I can see a Grid.Children.Add returns an int
and that's not what you want to set the content of the groupBox1
to.
据我所知,Grid.Children.Add返回一个int,而不是你想要将groupBox1的内容设置为。
An untested idea from me as a non WPF expert is to set the grid as the Content of your groupbox.
我作为非WPF专家的一个未经测试的想法是将网格设置为您的组框的内容。
grid1.Children.Add(textBlock1);
groupBox1.Content = grid1;
#2
0
For simple checkboxes i used this code :
对于简单的复选框,我使用此代码:
var container = new FlowLayoutPanel
{
FlowDirection = FlowDirection.TopDown,
Dock = DockStyle.Fill
};
myGroupBox.Controls.Add(container);
foreach (var myText in textList)
{
var checkBox = new CheckBox
{
Text = myText
};
container.Controls.Add(checkBox);
}
Of course the foreach statement is just for the example :)
当然foreach语句仅用于示例:)