Frode Lillerud
Frode Lillerud

Reputation: 7394

Add WPF control at runtime

I've written a WPF UserControl, and want to add one or more of it to my Window at runtime when I click a button. How can I do that?

Edit: Further specification I want to add the usercontrols to a Canvas, and put in a absolute position. The canvas is a drawing of the floors in my house, and each usercontrol has properties to indicate where in the house it is positioned. So I want all the controls to be positioned in the correct position on the canvas.

I'm thinking something like this

var light = new LightUserControl(2);
HouseCanvas.Children.Add(light); // this should be positioned in a specific place

Upvotes: 32

Views: 79946

Answers (6)

Ramiro Berrelleza
Ramiro Berrelleza

Reputation: 2405

In case you want to add the control to a Grid instead of a Canvas you can specify all the Grid properties through the Grid static class as follows:

Label newLabel = new Label();
newLabel.Content = "The New Element";
Main.Children.Add(newLabel);
Grid.SetColumn(newLabel, 0);
Grid.SetRow(newLabel, 0);

Upvotes: 13

Cassius
Cassius

Reputation: 169

Just complementing the answer:

for (i = 1; i <= buttoncount; i++)
{
    Button mybutton = new Button();
    Grid1.Children.Add(mybutton);
    mybutton.Height = 100;
    mybutton.Width = 100;
    mybutton.Name = "button" + i;
    mybutton.Content = mybutton.Name;
    mybutton.Click += button_Click;
}

private void button_Click(object sender, RoutedEventArgs e)
{
    // do something
}

Upvotes: 0

dexiang
dexiang

Reputation: 1413

    public static void AddChild(this Visual parent, UIElement child)
    {
        if (InternalAddChild(parent, child))
        {
            return;
        }
        throw new NotSupportedException();
    }
    private static bool InternalAddChild(Visual parent, UIElement child)
    {
        Panel panel = parent as Panel;
        if (panel != null)
        {
            panel.Children.Add(child);
            return true;
        }
        for (int i = VisualTreeHelper.GetChildrenCount(parent) - 1; i != -1; i--)
        {
            Visual target = VisualTreeHelper.GetChild(parent, i) as Visual;
            if (target != null && InternalAddChild(target, child))
            {
                return true;
            }
        }
        return false;
    }

Upvotes: 0

Dhruv Singhal
Dhruv Singhal

Reputation: 56

My solution:

for (i = 1; i <= buttoncount; i++)
{
    Button mybutton = new Button();
    Grid1.Children.Add(mybutton);
    mybutton.Height = 100;
    mybutton.Width = 100;
    mybutton.Name = "button" + i;
    mybutton.Content = mybutton.Name;
}

Upvotes: 2

Ian Oakes
Ian Oakes

Reputation: 10253

After you add the your control to the Canvas you need to specify the top and left co-ordinates using the Canvas.Top and Canvas.Left attached properties as follows.

var light = new LightUserControl(2);
HouseCanvas.Children.Add(light);
Canvas.SetLeft(light, 20);
Canvas.SetTop(light, 20);

Upvotes: 46

Jobi Joy
Jobi Joy

Reputation: 50038

Add a StackPanel to the window and on each button click,

 _stackPanel.Children.Add(new YourControl());  

You can do this in many ways.

Upvotes: 2

Related Questions