Find a child control from a parent control and a child Control
/// <Summary>
/// Refresh the layer list and add the clip child element time control TimeRange
/// </Summary>
Public void RefreshListView ()
{
For (int I = 0; I <lstCoverage. Items. Count; I ++) // lstCoverage is a ListView control.
{
UIElement u = lstCoverage. ItemContainerGenerator. ContainerFromIndex (I) as UIElement;
If (u = null)
{
Debug. WriteLine ("the retrieved UIElement is blank !!...... ");
Continue;
}
ClsCoverageInfo tmCvr = lstCoverage. Items [I] as clsCoverageInfo; // clsCoverageInfo bind a data source
List <MaterageTiemList> btnList = FindVisualChild <MaterageTiemList> (u );
If (btnList. Count = 0)
{
Debug. WriteLine ("search failed ...... ");
}
Else
{
BtnList [0]. SetView (tmCvr. m_strCoverageMeterals );
Debug. WriteLine ("search successful ...... ");
}
}
}
/// <Summary>
/// Use visualtreehelper to find the child object of the object
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "obj"> </param>
/// <Returns> </returns>
List <T> FindVisualChild <T> (DependencyObject obj) where T: DependencyObject
{
Try
{
List <T> TList = new List <T> {};
For (int I = 0; I <VisualTreeHelper. GetChildrenCount (obj); I ++)
{
DependencyObject child = VisualTreeHelper. GetChild (obj, I );
If (child! = Null & child is T)
{
TList. Add (T) child );
}
Else
{
List <T> childOfChildren = FindVisualChild <T> (child );
If (childOfChildren! = Null)
{
TList. AddRange (childOfChildren );
}
}
}
Return TList;
}
Catch (Exception ee)
{
// MessageBox. Show (ee. Message );
Return null;
}
}