How does WPF obtain the control in controltemplate?

Source: Internet
Author: User
1   Checkbox cbselect  =  (Checkbox) (listboxview. items [I]  As  Listboxitem). template. findname (  "  Cbselect  "  , Listboxview. items [I]  As  Listboxitem );
2 Textblock textblackconnpointnum = (Textblock) (listboxview. items [I] As Listboxitem). template. findname ( " Textblackconnpointnum " , Listboxview. items [I] As Listboxitem );
3 Textblock textblackconnpointnam = (Textblock) (listboxview. items [I] As Listboxitem). template. findname ( " Textblackconnpointname " , Listboxview. items [I] As Listboxitem );
4 ComboBox cbgroup = (ComboBox) (listboxview. items [I] As Listboxitem). template. findname ( " Cbgroup " , Listboxview. items [I] As Listboxitem );
5 Checkbox cbpublic = (Checkbox) (listboxview. items [I] As Listboxitem). template. findname ( " Cbpublic " , Listboxview. items [I] As Listboxitem );

Corresponds to XAML:

 1   <  Contentcontrol  >  
2 < Grid >
3 < Checkbox Name = "Cbselect" Margin = "2, 0, 0" Verticalalignment = "Center" Ischecked =" {Binding isselected, mode = twoway, relativesource = {relativesource templatedparent }} " <Contentpresenter margin = "2" />
4 5 < Textblock Width = "80" Name = "Textblackconnpointnum" Textalignment = "Center" Text =" {Binding Path = connpointnum} " />
6   < Textblock Width = "150" Name = "Textblackconnpointname" Text =" {Binding Path = connpointname} " Textalignment = "Center" />
7 ......
8 </ Grid >
9 </ Contentcontrol >

In controltemplate, the most important thing to find a control is to initialize the control to the visualtree. Therefore, it is possible that the control has not been loaded to the visualtree.

You can use either of the following methods to forcibly refresh the template control to the visualtree.

The first type isUpdatelayout () method.

But the msdn prompt is:If a large number of elements exist in the UI, frequent calls to invalidatearrange (especially frequent calls to updatelayout) will have a significant impact on performance. UnlessCodeTo call other APIs in the future, the layout must be accurate. Otherwise, avoid calling this method.

Solution 2:

  Private delegate void noargdelegate ();

Public static void refresh (dependencyobject OBJ)
{

OBJ. Dispatcher. begininvoke (system. Windows. Threading. dispatcherpriority. applicationidle, (noargdelegate) delegate {});

}

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.