locked
Adding child to Canvas RRS feed

  • Question

  • Hi all,
    How can I add a child element to Canvas from managed code and set x, y coordinates to it?
     

    Thanks,

    Narek 

     

     

    Friday, January 25, 2008 9:42 AM

Answers

  • create a new silverlight project and overwrite default page_load of your xaml file with this..

     

    public void Page_Loaded(object o, EventArgs e)

    {

    TextBlock tb = new TextBlock();

    tb.SetValue(Canvas.LeftProperty, 100);

    tb.SetValue(Canvas.TopProperty, 100);

    tb.Text = "Child Control";

    this.Children.Add(tb);

    }

    when you will run your project you will find your new Textblock child control in the canvas.

    and 100px left and 100px top.

     

    "if this answer helped you then please mark it answered" 

     

    Friday, January 25, 2008 9:55 AM

All replies

  • create a new silverlight project and overwrite default page_load of your xaml file with this..

     

    public void Page_Loaded(object o, EventArgs e)

    {

    TextBlock tb = new TextBlock();

    tb.SetValue(Canvas.LeftProperty, 100);

    tb.SetValue(Canvas.TopProperty, 100);

    tb.Text = "Child Control";

    this.Children.Add(tb);

    }

    when you will run your project you will find your new Textblock child control in the canvas.

    and 100px left and 100px top.

     

    "if this answer helped you then please mark it answered" 

     

    Friday, January 25, 2008 9:55 AM
  • how do you give a name to this child you have added to the canvas?

    Saturday, January 26, 2008 1:53 PM
  • Rectangle rt = new Rectangle();

    rt.SetValue<string>(DependencyObject.NameProperty, "test");

    Saturday, January 26, 2008 2:04 PM
  • tr.Name = "test";  

    Name property should work. 

    Saturday, January 26, 2008 2:07 PM
  • isn't that Name Property readonly?

    Saturday, January 26, 2008 2:17 PM
  • isn't that Name Property readonly?
     

    Yes. You are right. I'm not so sure how come this property is readonly. Width or Height properties are not readonly.  

    Saturday, January 26, 2008 2:51 PM
  • yeah that is really strange. any expaination why this is readonly would be interesting cuz it makes no sense to me ... why shouldn't someone want to change the name?

    i hope this issue will be changed in 2.0 beta ;)

    Saturday, January 26, 2008 2:55 PM
  • actually the following works for me to change the name:

    myShape.SetValue<string>(DependencyObject.NameProperty, "myShapesName");

     

     

    Saturday, January 26, 2008 3:35 PM
  • Hi Reza,

    You are NOT reading the whole thread, don't you? Thats why you are no idea what we are talking about..  

    Saturday, January 26, 2008 3:41 PM
  • apparently.....i have no idea what you guys are talking about. yes, i definitely didnt understand that you were wondering why you cant set the Name property directly, eg. myShape.Name = "blah", and why name is a readonly property. Yea, i definitely am not reading the whole thread.

    Saturday, January 26, 2008 3:49 PM