See how to parse the position of elements in the visual tree and logical tree in standard Silverlight and Silverlight for Windows Phone. In the past, I wrote an article about the same intent in the Windows 8 winrt framework: winrt/Metro: the position of the parsing element in the visual tree and logical tree. Since winrt is very similar to Silverlight in the interface xaml, the code in the winrt article is basically applicable to Silverlight. Because neither winrt nor Silverlight has the logicaltreehelper type compared to WPF, the upper level of uielement is not the visual type, and frameworkelement does not have the logicalchildren attribute ......
In Silverlight:
The two lists are the same, simple and clear (not nesting a bunch of things like WPF)
In Windows Phone:
Interface XAML:
<Grid>
<Grid. columndefinitions>
<Columndefinition/>
<Columndefinition/>
</Grid. columndefinitions>
<Grid. rowdefinitions>
<Rowdefinition Height = "Auto"/>
<Rowdefinition/>
</Grid. rowdefinitions>
<Textblock> logical tree </textblock>
<Textblock grid. Column = "1"> visual tree </textblock>
<ListBox name = "lbxlogical" grid. Row = "1"/>
<ListBox name = "lbxvisual" grid. Row = "1" grid. Column = "1"/>
</GRID>
Obtain the code of the visual tree and logical tree:
Ienumerable <dependencyobject> getvisualtree (dependencyobject OBJ)
{
VaR list = new list <dependencyobject> () {OBJ };
VaR res = OBJ;
While (RES = visualtreehelper. getparent (RES ))! = NULL)
List. Add (RES );
Return list. asenumerable (). Reverse ();
}
Ienumerable <dependencyobject> getlogicaltree (dependencyobject OBJ)
{
VaR list = new list <dependencyobject> () {OBJ };
VaR res = OBJ;
While (RES = getparent (RES ))! = NULL)
List. Add (RES );
Return list. asenumerable (). Reverse ();
}
Dependencyobject getparent (dependencyobject OBJ)
{
VaR FF = OBJ as frameworkelement;
If (FF! = NULL)
Return ff. parent;
Return NULL;
}
Finally, execute in the corresponding loaded event (PARSE two listboxes for example respectively)
Lbxlogical. itemssource = getlogicaltree (lbxlogical). Select (D => D. GetType (). Name );
Lbxvisual. itemssource = getvisualtree (lbxvisual). Select (D => D. GetType (). Name );