Use SilverLight to traverse parent and child Controls

Source: Internet
Author: User

Use silverlight to traverse parent and child Controls

It is really troublesome to find elements in the datagrid in silverlight. No rows object can be traversed. It is quite useful to find these methods from the Internet:

Public class vthelper ()
{
Public t getparentobject <t> (dependencyobject obj, string name) where t: frameworkelement
{
Dependencyobject parent = visualtreehelper. getparent (obj );

While (parent! = Null)
{
If (parent is t & (t) parent). name = name | string. isnullorempty (name )))
{
Return (t) parent;
}
Parent = visualtreehelper. getparent (parent );
}
Return null;
}

Public t getchildobject <t> (dependencyobject obj, string name) where t: frameworkelement
{
Dependencyobject child = null;
T grandchild = null;

For (int I = 0; I <= visualtreehelper. getchildrencount (obj)-1; I ++)
{
Child = visualtreehelper. getchild (obj, I );

If (child is t & (t) child). name = name | string. isnullorempty (name )))
{
Return (t) child;
}
Else
{
Grandchild = getchildobject <t> (child, name );
If (grandchild! = Null)
Return grandchild;
}
}
Return null;
}

Public list <t> getchildobjects <t> (dependencyobject obj, string name) where t: frameworkelement
{
Dependencyobject child = null;
List <t> childlist = new list <t> ();

For (int I = 0; I <= visualtreehelper. getchildrencount (obj)-1; I ++)
{
Child = visualtreehelper. getchild (obj, I );

If (child is t & (t) child). name = name | string. isnullorempty (name )))
{
Childlist. add (t) child );
}
Childlist. addrange (getchildobjects <t> (child ,""));
}
Return childlist;
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.