Dvlnblk

If I have a media player how can I clone it to make an exact copy of it


Thanks



Re: Windows Presentation Foundation (WPF) Clone an Object

Josh Smith

You cannot clone an element, such as MediaPlayer. WPF elements do not support cloning. You will need to make a new instance of the element and then manually copy the properties you need "cloned."




Re: Windows Presentation Foundation (WPF) Clone an Object

Dvlnblk

After looking around I realized that was probably going to be the case. Now my problem is that the thing I want to clone is a User Control and being control via a Class. I know how to create a new instance of my media player user control but how can I add it to myCanvas (Canvas name) if that Canvas, named myCanvas is not in the scope of my UserControl Class Thanks for the help...



Re: Windows Presentation Foundation (WPF) Clone an Object

WPCoder

FrameworkElement.FindName("myCanvas") could work if you scope the search properly. If it's an external class that initiates the clone, why not have that code clone and parent it to the same parent as the current instance






Re: Windows Presentation Foundation (WPF) Clone an Object

Dvlnblk

If it's an external class that initiates the clone, why not have that code clone and parent it to the same parent as the current instance If it's an external class that initiates the clone, why not have that code clone and parent it to the same parent as the current instance

Can you give me an example of how you can do this

Thanks for the help.





Re: Windows Presentation Foundation (WPF) Clone an Object

Jonathan.Peppers

If you're creating your control in Xaml, and all you need to do is add to more than one parent control, you can do the following:

Code Snippet

<Grid.Resources>

<YourControl x:Key="MyControl" x:Shared="False" />

</Grid.Resources>

Setting x: Shared to false allows the control to be added to more than one Parent from code.

So you could do something like this in C#:

Code Snippet

YourControl control = FindResource("MyControl") as YourControl;

MyGrid.Children.Add(control);

MyCanvas.Children.Add(control);

All without copying your control at all. (I think it's done internally somehow, however).

Hope that's what your looking for.





Re: Windows Presentation Foundation (WPF) Clone an Object

Dvlnblk

Awesome, I will check it out. Thanks!



Re: Windows Presentation Foundation (WPF) Clone an Object

WPCoder

It's not a clone of the usercontrol and any current properties. It will only be a new copy of the state that was set in the resources, so this may not be what you want (it isn't a "Live" clone of current state).

For my previous suggestion --

If you have a second class:

Code Snippet

internal static class Cloner

{

public static void Clone(MyUserControl c) {

MyUserControl newC = new MyUserControl();

// copy stuff from c to newC

c.Parent.Children.Add(newC);

}

}

Something like that ... this would be a 'live' copy that copies all of the settings you want from a running instance to a new instance.