[Silverlight] How To Get The objects defined in the template

Source: Internet
Author: User

In the past, the technical problem of Silverlight has not been solved, that is, if you get the reference of each object defined in the template in code behind. Refer to XAMLCode:

Code

< ListBox X: Name = "Lbx_listbox"
<ListBox. itempanel >

<ItemspaneltemplateX: Key= "Filelistitemspaneltemplate">
<Controlstoolkit: wrappanelX: Name= "Wrp_listwrapper"

Width= "450"

Orientation= "Horizontal" />
</Itemspaneltemplate>
</ListBox. itempanel>
</ListBox>

This Code defines a list. In the list, I use wrappanel as the container for the list items. It is obviously used to implement the automatic line feed function of the horizontal list items. But now I hardcoded the 450 width to wrappanel, so wrappanel does not automatically follow the changes when the list control grows or decreases. Therefore, I must implement an automatic update mechanism. You can use data binding or the sizechanged event of ListBox. However, no matter which method is used, I must obtain the reference of this wrappanel object.

I intentionally gave this wrappanel a name attribute (value: wrp_listwrapper), which will not cause compilation errors or runtime exceptions. However, despite having a name attribute, I still cannot obtain the wrappanel reference from code behind because it is an object defined in the template.

The method for obtaining reference is the loaded event. Refer to the following code for improvement:

Code

< ListBox X: Name = "Lbx_listbox"
<ListBox. itempanel >

<ItemspaneltemplateX: Key= "Filelistitemspaneltemplate">
<Controlstoolkit: wrappanelX: Name= "Wrp_listwrapper"

Width= "450"

Orientation= "Horizontal"

Loaded= "Wrp_listwrapper_loaded"/>
</Itemspaneltemplate>
</ListBox. itempanel>
</ListBox>

The highlighted code is used to register and process the loaded event of wrappanel.Program. This event is triggered only once when the list is loaded. In code behind, use the following code to save the wrappanel reference:

Private Wrappanel m_listwrapper;

Private VoidWrp_listwrapper_loaded (ObjectSender, routedeventargs E)
{
M_listwrapper=SenderAsWrappanel;

}

In this simple way, you can save the reference of the wrappanel object defined in the template. This method can also be used for other similar control objects defined in the template. However, it should be noted that

Datatemplate templates are generally used by multiple data items. For example, datatemplate in a ListBox is used by all list items in The ListBox. In this case, the loaded event of the control defined in the template will occur multiple times, each time triggered by a different list item. However, there is only one event handler, so you must identify each instance in the program code.

For example:

Code

< ListBox X: Name = "Lbx_listbox"
<ListBox. itemtemplate >

<Datatemplate>

<ImageSRC= {Binding}Loaded= "Image_loaded"/>

</Datatemplate>
</ListBox. itemtemplate>
</ListBox>

Each list item in The ListBox is an image. If we use the same method to obtain the reference of this image in code behide:

Private Ilist < Image > M_imgs = New List < Image > ();

private void image_loaded ( Object sender, routedeventargs E)
{< br> m_imgs.add (sender as image );

}

here, an image list set is used to save the image in the template, because multiple list items run the handler multiple times. If you want to select a specified image, you must add the filter code.

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.