Window1.xaml

    <Canvas x:Name="mycanvas">
    <Button Width="100" Height="50" Canvas.Left="350" Canvas.Top="50" Click="Button_Click">Click to Display</Button>
    <ContentControl Width="130"
                    Height="130"
                    Canvas.Top="150"
                    Canvas.Left="470" Name="cc1"
                    Template="{StaticResource DesignerItemTemplate}">
                    <Grid x:Name="g1">
                    <Ellipse Fill="LightGray" Stroke="Black" Stretch="Fill" Name="myElli" IsHitTestVisible="False"/>
    <TextBlock VerticalAlignment="Center" TextAlignment="Center" Height="61.96" Margin="10,35.02,10,33.02">Simple Ellipse</TextBlock>
                    </Grid>
    </ContentControl>
    <ContentControl Width="130"
                    Height="130"
                    Canvas.Top="150"
                    Canvas.Left="150"
                    Template="{StaticResource DesignerItemTemplate}">
                    <Grid>
                    <Path Fill="LightGray" Stroke="Black" Name="myPath"
                    Data="M 0,5 5,0 10,5 5,10 Z"
                    Stretch="Fill"
                    IsHitTestVisible="False"/>
    <TextBlock VerticalAlignment="Center"  TextAlignment="Center">Simple Path</TextBlock>
                    </Grid>
                </ContentControl>
    </Canvas>

Hello,
I'm quite a newbie in WPF. I started programming on WPF a few days ago.
I'm working on creation of certian shapes on WPF.
I have created two shapes. I want to create copies of these shapes and place them on the same canvas. I would like to do this by capturing event of the Button.
It would also be convinient if the newly created shapes have different text inside them instead of the default text which I have currently placed.
Is there a way to do this? How should I handle this using C# code? Am I on the right track in creation of this shapes? Any better way to do this would be very helpful.

Thanks in advance!

Here's one way to add a shape:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Ellipse NewEllipse = new Ellipse();
        NewEllipse.Name = "newellipse";
        NewEllipse.Height = 100;
        NewEllipse.Width = 200;
        NewEllipse.Stroke = Brushes.Black;
        NewEllipse.Fill = Brushes.DarkRed;            
        canvas1.Children.Add(NewEllipse);

    }

Thank you for your reply tinstaafl.
But the thing is, I have already created an Ellipse using XAML tags.
Is there any way to use the properties of the same Ellipse as a new object again and add it to the same Canvas?

Perhaps something like this, where "ellispse1" in the name of the ellipse you defined in XAML.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Ellipse copyel = EllipseCloneProperties(ellipse1);
            // set your positioning properties
            canvas1.Children.Add(copyel);
        }
        private Ellipse EllipseCloneProperties(Ellipse source)
        {
            Ellipse clone = new Ellipse();

            foreach (System.Reflection.PropertyInfo pi in typeof(Ellipse).GetProperties())
            {
                if (pi.CanWrite && pi.CanRead)
                {
                    pi.SetValue(clone, pi.GetValue(source, null), null);
                }
            }

            return clone;
        }

Thanks TnTinMN for your reply. It works !
But as I had placed my Ellipse within the ContentControl, I had to clone the ContentControl instead of the Ellipse that I had used in my XAML file.
My updated code is:

private void btn1_Click_1(object sender, RoutedEventArgs e)
        {
            ContentControl copy1 = EllipseCloneProperties(MainEli);
            Canvas.SetLeft(copy1, 300);
            Canvas.SetTop(copy1, 350);
            copy1.Width = 150;
            copy1.Height = 150;
            // set your positioning properties
            MainCanvas.Children.Add(copy1);
        }
        private ContentControl EllipseCloneProperties(ContentControl source)
        {
            ContentControl clone = new ContentControl();
            foreach (System.Reflection.PropertyInfo pi in typeof(ContentControl).GetProperties())
            {               
                if (pi.CanWrite && pi.CanRead)
                {
                    pi.SetValue(clone, pi.GetValue(source, null), null);
                }
            }
            return clone;
        }

Once the clone is complete, the original ContentControl will not have Ellipse within it.
This is the original ContentControl before Cloning.
These are the ContentControls after cloning.
What seems to be the problem? What am I doing wrong?
Why does the cloned ContentControl not retain the Grid and Ellipse within it? Is there a different way to do it?

The routine that I posted does not clone Children nor Content. It may be possible to write a generic cloner, but write know I do not see an easy way to do so.

It is be fairly easy to write a simple cloner for the pattern of ContentControl <- Grid <- Shape.

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            ContentControl cc = ContentControlCloner(ContentControl1);
            cc.Margin = new Thickness(100, 0, 0, 0);
            Canvas1.Children.Add(cc);
        }

        private Shape ShapeCloneProperties(Shape source)
        {
            Shape clone = null;

            switch (source.GetType().Name)
            {
                case "Ellipse":
                    clone = new Ellipse();
                    break;
                case "Line":
                    clone = new Line();
                    break;
                case "Path":
                    clone = new Path();
                    break;
                case "Polygon":
                    clone = new Polygon();
                    break;
                case "Polyline":
                    clone = new Polyline();
                    break;
                case "Rectangle":
                    clone = new Rectangle();
                    break;
                default:
                    throw new ArgumentException("Invalid shape");
            }

            foreach (System.Reflection.PropertyInfo pi in source.GetType().GetProperties())
            {
                if (pi.CanWrite && pi.CanRead)
                {
                    pi.SetValue(clone, pi.GetValue(source, null), null);
                }
            }

            return clone;
        }

        private ContentControl ContentControlCloner(ContentControl source)
        {
            ContentControl clone = new ContentControl();
            clone.Name = "fred";

            foreach (System.Reflection.PropertyInfo pi in typeof(ContentControl).GetProperties())
            {

                if (pi.Name == "Content")
                {
                    Grid gr = (Grid)source.Content;

                    Grid gridclone = GridCloneProperties(gr);

                    gridclone.Children.Clear();
                    Shape sh = ShapeCloneProperties((Shape)gr.Children[0]);
                    gridclone.Children.Add(sh);
                    clone.Content = gridclone;

                }
                else
                {
                    if (pi.CanWrite && pi.CanRead)
                    {
                        pi.SetValue(clone, pi.GetValue(source, null), null);
                    }

                }
            }
            return clone;

        }

        private Grid GridCloneProperties(Grid source)
        {
            Grid clone = new Grid();

            foreach (System.Reflection.PropertyInfo pi in typeof(Grid).GetProperties())
            {
                if (pi.CanWrite && pi.CanRead)
                {
                    pi.SetValue(clone, pi.GetValue(source, null), null);
                }
            }

            return clone;
        }

    }

Thanks TnTinMN !
That definately does the job. I've still got a few problems. But I think i'll be able to continue.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.