locked
Unique name for elements inside canvas RRS feed

  • Question

  • Hi all,

    To get a particular element inside a canvas , I have to use canvasname.children[index]. But if I delete any particular element the index of the successive elements gets updated. So I would like to know whether we can give Unique Id for the elemets inside a canvas.

    Monday, June 13, 2011 5:17 AM

Answers

  • Hi,

    I think you could assign the Name property to each child before you add it into a canvas.

    If your child may not have the same type, you could cast the child element as FrameworkElement then assing its name property.

    For example,

    var rect = new Rectangle();

    ( rect as FrameworkElement ).Name = "myRect1";

     

    Or, you could get the child element from your canvas then set its name, too:

    ( myCanvas.Children[ index ] as FrameworkElement ).Name = string.Format( "child_{0}" , index );

     

    May this information is helpful to you.

     

     

     


    Ouch Liu
    Welcome to visit by blog: Ouch@點部落
    Welcome to join the Designer x Developer group on Facebook: Facebook 設計x程式 社團
    • Marked as answer by chaaraan Thursday, June 16, 2011 1:10 AM
    Monday, June 13, 2011 10:14 AM

All replies

  • Hi,

    I think you could assign the Name property to each child before you add it into a canvas.

    If your child may not have the same type, you could cast the child element as FrameworkElement then assing its name property.

    For example,

    var rect = new Rectangle();

    ( rect as FrameworkElement ).Name = "myRect1";

     

    Or, you could get the child element from your canvas then set its name, too:

    ( myCanvas.Children[ index ] as FrameworkElement ).Name = string.Format( "child_{0}" , index );

     

    May this information is helpful to you.

     

     

     


    Ouch Liu
    Welcome to visit by blog: Ouch@點部落
    Welcome to join the Designer x Developer group on Facebook: Facebook 設計x程式 社團
    • Marked as answer by chaaraan Thursday, June 16, 2011 1:10 AM
    Monday, June 13, 2011 10:14 AM
  • Hi Liu,

    Thanks for the reply, but my concern is consider there are three circles and they are connected by two lines , So the index will be circle1(0 - index),circle2(1),circle3(2),Line1(3) Line2(4). When the line1 is removed from the canvas the index of Line2 gets updated to 3 . But I want to know whether there is any way for maintaining a unique name for an element without getting updated

     

     

    Regards,

    Charan

    Tuesday, June 14, 2011 5:10 AM
  • Hi Charan,

    I agree with Ouch Liu. The straightforward solution to maintain a unique name for an element is to use the Name property (or anything that can id the element) instead of using the index.

    Alternatively, if you insist to using the index and do not want the index to change, then do not remove anything from the Canvas. You can use the Visibility property to hide an element instead of removing it.

    If you still have any questions or concerns about this issue, please feel free to let me know.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 15, 2011 8:11 AM