Question

I'm creating dynamic ComboBoxes and I also want to add to each ComboBox a ColorPicker. But unfortunately I don't know how.

Any advices?

This is the code if it helps:

for (int i = 1; i < int.Parse(shapes)+1; i++)
                    {
                        System.Windows.Controls.ComboBox box = new ComboBox();

                        box.Height = 23;
                        box.Width = 70;
                        box.Items.Add("------");
                        box.Items.Add("Sphere");
                        box.Items.Add("Line");
                        box.Items.Add("Plane");
                        box.Items.Add("Pyramid");
                        box.Items.Add("Cylinder");
                        box.Items.Add("Cube");
                        box.SelectedIndex = 0;

                        box.Margin = new Thickness(0, -285 + i * 62, 375, 0);
                        box.Name = "box" + i.ToString();

                        grid2.Children.Add(box);
                    }

Thanks.

Was it helpful?

Solution

I see two ways to solve your problem :

UserControl

Your comboboxes seem to always have a colorpicker associated to them, so you should create a custom user control in XAML with just a colorpicker and the combobox. Then you'll be able to work with it as if it was a classic WPF control.

StackPanel

You can instanciate a new StackPanel (Orientation horizontal) for every iteration and add the combobox + colorpicker to its Children elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top