UserControl throwing: Element is already the child of another element

I am developing an UWP app in which I required a Accordion. Since, UWP toolkit does not provides the accordion control therefore, I have create my own Accordion control. And it works like a charm.

The problem is, when I create multiple instances of my usercontrol then it throws an exception. I have shared a stripped down sample on the following link:


ANSWERS:


I'm not entirely sure what the problem is. (I think it's to do with having a static backing property that defaults to an object and is then included in each control but can't prove it.)

However, there's an easy fix.
Instead of using your MydesqAccordionUserControl.xaml which basically wraps and then tries to re-implement an ItemsControl you can inherit from it.
Remove MydesqAccordionUserControl.xaml and MydesqAccordionUserControl.xaml.cs rom your project and replace them with a class containing the code below. Problem sorted.

using Windows.UI.Xaml.Controls;

namespace MydesqAccordionUserControl
{
    public class MydesqAccordionControl : ItemsControl
    {
        public void CollapseOtherItems(MydesqAccordionItem item)
        {
            if (Items != null)
            {
                foreach (var mydesqAccordionItem in Items)
                {
                    if (!mydesqAccordionItem.Equals(item))
                    {
                        (mydesqAccordionItem as MydesqAccordionItem)?.CollapseContent();
                    }
                }
            }
        }
    }
}


 MORE:


 ? UWP/WinRT: How to use VisualState Triggers to change the styling of all controls of a certain type?
 ? UWP/WinRT: How to use VisualState Triggers to change the styling of all controls of a certain type?
 ? UWP/WinRT: How to use VisualState Triggers to change the styling of all controls of a certain type?
 ? UWP/WinRT: How to use VisualState Triggers to change the styling of all controls of a certain type?
 ? Adaptive trigger doesn't work in a user control
 ? UWP/WinRT: How to move a button when the window is resized into a new position?
 ? UWP/WinRT: Is it possible to change a Style using an AdaptiveTrigger?
 ? How to clear the date bound to a CalendarDatePicker if certain conditions occur in UWP?
 ? How to trigger a method in foreground when internet is available in UWP?
 ? Force Image control to re-download the image