Find a child control from a parent control and a child Control

Source: Internet
Author: User

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

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.