Silverlight and Windows Phone: Position of parsing elements in the visual tree and logical tree

Source: Internet
Author: User

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 );

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.