Search code examples
xamlwindows-phone

programatically duplicate xaml control in WP


I'm not getting how I could duplicate a XAML element pgrogramatically. For example if I define on XAML a rectangle om the middle of the screen, the user presses a button and I would like to duplicate this rectangle with all properties of it and then put it to the right of the first one, should this be possible?


Solution

  • Sure - just do a deep copy of the properties you want inserted and then add it to the panel. Assuming your controls are in a StackPanel with horizontal orientation:

    Rectangle newRect = new Rectangle()
    {
        Width = oldRect.Width,
        Height = oldrect.Height,
        Stroke = oldRect.Stroke,
        // repeat for other values you want the same
    };
    myStackPanel.Children.Add(newRect);