32

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

6 Answers 6

46

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);
Sign up to request clarification or add additional context in comments.

6 Comments

After hours of working with WPF I fail to see how it is superior to WinForms. I mean really, they seem to have made simple things frustratingly difficult, complex, and implemented atop layer upon layer of abstraction. This stuff makes me want to punch myself in the face. /rant /troll /useless comment
Totally agree Ed - WPF is prettier than WinForms, and has useful features for skinning, but it's hard to learn (undiscoverable), pushes lots of errors from compile-time to run-time, etc., and other than nice graphics it has little to show for its enormous complexity. I could think of tons of ways its design could have been improved.
@EdS. Impressive, literally hours with WPF and you've come to that conclusion.
@Erode: Yeah, I admit; it was a bit early to make that statement. I like WPF for the power it gives you. It also makes tasks which should be simple very difficult. I don't think it's a great UI platform, but not terrible either, and this is after 8 months of working on a WPF project.
@Erode: "For now" indeed, we'll see how WPF fares after the release of Windows 8... Also, I kind of remember making that comment. I believe it was made out of frustration after spending hours trying to get (what should have been) some simple mouse event handlers working.
|
13

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

3 Comments

This isn't necessarily what the original questioner wanted, but it was a help to me. Thanks.
label in the second line has to be newLabel right?
@kame I'm guessing so
2

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

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

You can do this in many ways.

1 Comment

Thanks, the .Children property was the one I needed
2

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

Comments

0
    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;
    }

Comments

0

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
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.