Hi,@Levi Para. Welcome Microsoft Q&A.
The whole idea is that if an element already has a logical parent, it cannot be assigned as a child of another element.
Such as the errors you encounter.
If you want a new instance identical to an existing control instance you could use XamlWriter and XamlReader to serialize/deserialize the control.
UserControl:
<StackPanel Orientation="Horizontal">
<Label Content="Name" Width="100" Height="50" Background="LawnGreen"/>
<TextBox Width="100" Height="50" Background="AliceBlue" Text="li"/>
</StackPanel>
MainWindow.xaml:
<Grid x:Name="grid">
<local:UserControl1 x:Name="myUserControl"/>
<Button Content="copy" Width="100" Height="50" Click="Button_Click"/>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
//attempt to reference the element by x:name
Window newWin = new Window();
UserControl copy = XamlReader.Parse(XamlWriter.Save(myUserControl)) as UserControl;
newWin.Content = copy;
newWin.Show();
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.