31

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

45

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);
6
  • 33
    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
    – Ed S.
    Feb 16, 2011 at 1:36
  • 7
    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.
    – Qwertie
    Jun 1, 2011 at 0:06
  • 13
    @EdS. Impressive, literally hours with WPF and you've come to that conclusion.
    – erodewald
    Jun 5, 2012 at 14:28
  • 2
    @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.
    – Ed S.
    Jun 5, 2012 at 16:32
  • 3
    @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.
    – Ed S.
    Jun 5, 2012 at 21:50
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
  • 2
    This isn't necessarily what the original questioner wanted, but it was a help to me. Thanks.
    – hughdbrown
    Feb 11, 2009 at 14:44
  • 1
    label in the second line has to be newLabel right?
    – kame
    Nov 6, 2015 at 7:12
  • @kame I'm guessing so
    – Toby Smith
    Jan 19, 2016 at 4:06
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
  • Thanks, the .Children property was the one I needed Nov 22, 2008 at 9:31
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;
}
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;
    }
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
}

Your Answer

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

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