【亚洲必赢官网】从一个父级控件找到某多个子级控件,查找子控件和父控件方法

/// <summary>
/// 刷新图层列表,增多素材子成分时间控件TimeRange
/// </summary>
public void RefreshListView()
{
for (int i = 0; i < lstCoverage.Items.Count;
i++)      //lstCoverage是一个ListView 控件
{
UIElement u = lstCoverage.ItemContainerGenerator.ContainerFromIndex(i)
as UIElement;

从贰个父级控件找到某贰个子级控件,级控件某三个子

/// <summary>
/// 刷新图层列表,加多素材子成分时间控件TimeRange
/// </summary>
public void RefreshListView()
{
for (int i = 0; i < lstCoverage.Items.Count;
i++)      //lstCoverage是一个ListView 控件
{
UIElement u = lstCoverage.ItemContainerGenerator.ContainerFromIndex(i)
as UIElement;

if (u == null)
{
Debug.WriteLine(“获取的UIElement为空!!……”);
continue;
}

clsCoverageInfo tmCvr = lstCoverage.Items[i] as
clsCoverageInfo;      //clsCoverageInfo绑定数据源
List<MaterageTiemList> btnList =
FindVisualChild<MaterageTiemList>(u);
if(btnList.Count == 0)
{
【亚洲必赢官网】从一个父级控件找到某多个子级控件,查找子控件和父控件方法。Debug.WriteLine(“查找未果……”);
}
else
{
btnList[0].SetView(tmCvr.m_strCoverageMeterals);
Debug.WriteLine(“查找成功……”);
}

}
}

/// <summary>
/// 利用visualtreehelper寻觅对象的子级对象
/// </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;
}
}

/// summary /// 刷新图层列表,增添素材子成分时间控件TimeRange ///
/summary public void Refr…

1、查找某体系型的子控件,并回到多个List集结

if (u == null)
{
Debug.WriteLine(“获取的UIElement为空!!……”);
continue;
}

public List<T>
GetChildObjects<T>(DependencyObject obj, Type typename) where T :
FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();

clsCoverageInfo tmCvr = lstCoverage.Items[i] as
clsCoverageInfo;      //clsCoverageInfo绑定数据源
List<MaterageTiemList> btnList =
FindVisualChild<MaterageTiemList>(u);
if(btnList.Count == 0)
{
Debug.WriteLine(“查找未果……”);
}
else
{
btnList[0].SetView(tmCvr.m_strCoverageMeterals);
Debug.WriteLine(“查找成功……”);
}

for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) – 1;
i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

}
}

if (child is T && (((T)child).GetType() == typename))
                {
                    childList.Add((T)child);
                }
               
childList.AddRange(GetChildObjects<T>(child,typename));
            }
return childList;
        }

/// <summary>
亚洲必赢官网,/// 利用visualtreehelper寻觅对象的子级对象
/// </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;
}
}

调用:

List<Button> listButtons =
GetChildObjects<Button>(parentPanel, typeof(Button));

2、通过名称查找子控件,并赶回一个List会集

public List<T>
GetChildObjects<T>(DependencyObject obj, string name) where T :
FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();

for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) – 1;
i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

if (child is T && (((T)child).GetType() == name
|string.IsNullOrEmpty(name)))
                {
                    childList.Add((T)child);
                }
               
childList.AddRange(GetChildObjects<T>(child,name));
            }
return childList;
        }

调用:

List<Button> listButtons =
GetChildObjects<Button>(parentPanel, “button1”);

三、通过名称查找某子控件:

public T GetChildObject<T>(DependencyObject
obj, string name) where T : FrameworkElement
{
    DependencyObject child = null;
    T grandChild = null;

for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) – 1;
i++)
    {
        child = VisualTreeHelper.GetChild(obj, i);

if (child is T && (((T)child).Name == name |
string.IsNullOrEmpty(name)))
        {
return (T)child;
        }
else
        {
            grandChild = GetChildObject<T>(child, name);
if (grandChild != null)
return grandChild;
        }
    }
returnnull;
}

调用:

StackPanel sp =
GetChildObject<StackPanel>(this.LayoutRoot, “spDemoPanel”);

四、通过名称查找父控件

public T GetParentObject<T>(DependencyObject
obj, string name) where T : FrameworkElement
{
    DependencyObject parent = VisualTreeHelper.GetParent(obj);

while (parent != null)
    {
if (parent is T && (((T)parent).Name == name |
string.IsNullOrEmpty(name)))
        {
return (T)parent;
        }

        parent = VisualTreeHelper.GetParent(parent);
    }

returnnull;
}

调用:

Grid layoutGrid =
VTHelper.GetParentObject<Grid>(this.spDemoPanel,
“LayoutRoot”);

网站地图xml地图