1

I´m new to C# and also WPF. Im playing around with Canvas and Ellipses and stuff.

I created a method to create a new canvas:

    public void CreateCanvas(Canvas parentCanvas, int height, int width, double xCoord, double yCoord)
    {
        Canvas new_canvas = new Canvas();
        new_canvas.Background = new SolidColorBrush(Colors.Red);
        new_canvas.Height = height;
        new_canvas.Width = width;
        Canvas.SetLeft(new_canvas, xCoord);
        Canvas.SetTop(new_canvas, yCoord);
        parentCanvas.Children.Add(new_canvas);

    }

When I, for example, want to add another child to a canvas, created this way, I need the name of the canvas. So my guess is to make the canvas name a parameter also. Well.. no clue how to.

I want it to look like this.

public void CreateCanvas(Canvas parentCanvas, string canvasName, int height, int width, double xCoord, double yCoord)
{
    Canvas canvasName = new Canvas();
    canvasName.Background = new SolidColorBrush(Colors.Red);
    canvasName.Height = height;
    canvasName.Width = width;
    Canvas.SetLeft(canvasName, xCoord);
    Canvas.SetTop(canvasName, yCoord);
    parentCanvas.Children.Add(canvasName);

}

What I want to do is to add a textblock as a child (Children.Add) to the canvas i created by the method, after the canvas is allready created.

F.Con
  • 13
  • 4
  • 2
    That's just not possible and why would you even do it? As soon as you add the canvas to the children list and exit the method, the canvas name will be lost anyway. – Milster Sep 07 '17 at 07:57
  • i want to add canvas, or ellipses dynamicaly and create text inside them. Since i dont know how many canvas or forms i will need i thought a method to create new ones would help. Like : ( if something happens) create a new canvas there.. then i need that canvas name to further add Children if some other stuff happens – F.Con Sep 07 '17 at 08:01
  • 1
    Maybe you want something like `Dictionary`? But there could be a plethora of problems with that... what requirement are you trying to fullfill? – Corak Sep 07 '17 at 08:02
  • That's correct way to create method to add child canvas (the first code sample). But you probably mistaken what the name is. Is this "name" is the text inside canvas? – Renatas M. Sep 07 '17 at 08:07
  • no the text will be a textblock I add as a children to the created canvas. But since I dont know whats the name of the canvas, I will not be able to add a textblock as its children – F.Con Sep 07 '17 at 08:38

1 Answers1

0

Create Your Canvas like This:

public void CreateCanvas(Canvas parentCanvas,string CanvasName, int height, int width, double xCoord, double yCoord)
{
    Canvas new_canvas = new Canvas();           
    new_canvas.Name = CanvasName;  //assign Name here    
    new_canvas.Background = new SolidColorBrush(Colors.Red);
    new_canvas.Height = height;
    new_canvas.Width = width;
    Canvas.SetLeft(new_canvas, xCoord);
    Canvas.SetTop(new_canvas, yCoord);
    parentCanvas.Children.Add(new_canvas);
}

Then you can find Children by its NameProperty using:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

Source code Taken from Here

And to add textblock later just call:

foreach (Canvas child in FindVisualChildren<Canvas>(this).Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.Equals("MyCanvas")))
{
    child.Children.Add(new TextBlock() { Text = "Dynamimcally Added Textblock" });
}

And ofcourse you should know the name of the canvas which you are going to use later.

for Creating Canvas:

CreateCanvas(ParentCanvas,"MyCanvas", 200, 200, 0, 0);
tabby
  • 1,878
  • 1
  • 21
  • 39