利用silverlight 遍歷父子控件通用方法
silverlight中datagrid找元素,真是麻煩,沒有rows對象,無法遍歷。從網上找來這些方法,挺好用的:
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;
}
}