0

I add GroupBoxes to an ItemsControl dynamically using:

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);

Now I need to add content to this GroupBox, like a few TextBlocks created like:

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

But I can't figure out how to do that. Normally to a Grid or something like that I would just use .Children.Add(MyTextBlock), but that doesn't work here.

Also I have to be able to remove specific Items from the ItemsControl again (best would be by the name of the Item, name_ in this example).

1
  • See if anything in this MSDN Search Helps as far as finding your Controls, once you found them you can use myItemsControl.Items.Remove(object)
    – Mark Hall
    Commented Jun 29, 2012 at 0:27

2 Answers 2

2

Try something like that

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);
3
  • Any idea how to remove items from the ItemsControl by the name?
    – philkark
    Commented Jun 28, 2012 at 22:06
  • I am new to WPF, it is a silly question but I need to know, in th above answer what is mainwindow.canvas. while I place the above code in my page it is showing error in the mainwindow.canvas. what is mainwindow object in the above answer. 1) I need to create any canvas in the xaml(design) page?
    – Surya
    Commented Oct 22, 2014 at 4:23
  • Yea what is the data type of mainWindow? There is no property canvas of mainWindow
    – user5346812
    Commented May 19, 2016 at 19:06
0

The GroupBox has only a Content Property which is designed to hold a ContentPresentor. You can add a Grid/Canvas etc.. to the GroupBox and then add your content to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.