How do I copy a MenuItem I created in one ContextMenu and copy it so that I can use it in a second ContextMenu?
I tried to copy it directly and removing it, but I get Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.
foreach(MenuItem mi in menuOptions.Items) {
entityRightClick.Items.Add(mi);
menuOptions.Items.Remove(mi);
}
I tried grabbing the MenuItem using the ItemContainerGenerator, it gave me a blank MenuItem (Debugger says that it is null)
for(int i = 0; i < menuOptions.Items.Count; i++) {
MenuItem temp = new MenuItem();
temp = menuOptions.ItemContainerGenerator.ContainerFromIndex(i) as MenuItem;
entityRightClick.Items.Add(temp);
}
Based on your error message, I’d expect this to work (though I haven’t tested it):