locked
Clone an Object RRS feed

  • Question

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


    Thanks

    Wednesday, May 30, 2007 8:51 PM

Answers

  • 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.

    Thursday, May 31, 2007 5:02 PM

All replies

  • 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."
    Wednesday, May 30, 2007 9:51 PM
  • 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...
    Wednesday, May 30, 2007 9:57 PM
  • 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?

     

     

    Thursday, May 31, 2007 12:53 AM
  • 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.

    Thursday, May 31, 2007 4:53 PM
  • 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.

    Thursday, May 31, 2007 5:02 PM
  • Awesome, I will check it out.  Thanks!
    Thursday, May 31, 2007 5:56 PM
  • 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.

     

    Thursday, May 31, 2007 8:24 PM