0

我使用以下方法将 GroupBoxes 动态添加到 ItemsControl:

string name_ = "TestName", header_ = "TestHeader"
GroupBox MyGroupBox = new GroupBox { Name = name_, Header= header_, Width = 240, Height = 150, Foreground=new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)) };

MyItemsControl.Items.Add(MyGroupBox);

现在我需要向这个 GroupBox 添加内容,就像创建的几个 TextBlocks 一样:

TextBlock MyTextBlock = new TextBlock {Text = "test"};

但我不知道该怎么做。通常对于网格或类似的东西,我只会使用 .Children.Add(MyTextBlock),但这在这里不起作用。

此外,我必须能够再次从 ItemsControl 中删除特定项目(最好是项目的名称,在本例中为 name_)。

4

2 回答 2

2

尝试类似的东西

GroupBox groupBox1 = new GroupBox();
Grid grid1 = new Grid();
TextBlock MyTextBlock = new TextBlock {Text = "test"};
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height =  185;
grid1.Width =  160;
grid1.Children.Add(MyTextBlock);
groupBox1.Content = grid1;
mainWindow.canvas.Children.Add(groupBox1);
于 2012-06-28T21:31:06.420 回答
1

GroupBox 只有一个Content属性,用于保存 ContentPresentor。您可以将网格/画布等添加到 GroupBox,然后将您的内容添加到其中。

于 2012-06-28T21:28:55.933 回答