8,735
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获得指定元素的所有子元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static List<T> GetChildObjects<T>(DependencyObject obj) 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)
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child));
}
return childList;
}
//别忘记给你ComboBox起个名字(比如A)
ComboBox comboBox = null;
List<ComboBox> comboboxList = GetChildObjects<ComboBox>(你的DataGrid名字);
if(comboboxList!=null)
{
for(int i = 0;i<comboboxList.Count;i++)
{
if(comboboxList[i].Name.Equals("A"))
{
comboBox = comboboxList[i];
}
}
}