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