Helper methods
///<summary>
/// Attempts to find the child framework element recursively from a given parent root object.
///</summary>
///<param name="currentObject">the parent root object</param>
///<param name="childElementName">the name of the child element</param>
///<param name="childElement">the child element which was found</param>
private FrameworkElement GetChildElement(DependencyObject currentObject, string childElementName)
{
FrameworkElement childElement = null;
if (currentObject == null || string.IsNullOrEmpty(childElementName))
{
return null;
}
if (currentObject is FrameworkElement && (currentObject as FrameworkElement).Name == childElementName)
{
childElement = currentObject as FrameworkElement;
return childElement;
}
foreach (object child in LogicalTreeHelper.GetChildren(currentObject))
{
if (child is DependencyObject)
{
childElement = GetChildElement(child as DependencyObject, childElementName);
if (childElement != null)
break;
}
}
return childElement;
}
对于微软的itemscontrol,获取其dataTemplate
/// Get all the visual childs in the current item of ItemsControl
///</summary>
///<param name="itemsControl">The items control which the current item belongs to.</param>
///<param name="currentItem">The current item in the items control.</param>
///<param name="visualChildName">The spcified visual child's name</param>
///<returns>Return all the visual childs in the current item.</returns>
private childType GetVisualChildrenInDataTemplate<childType>(ItemsControl itemsControl, object currentItem, string visualChildName) where childType : class
{
ContentPresenter itemContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(currentItem) as ContentPresenter;
if (itemContainer != null)
{
object visualChild = null;
DataTemplate dataTemplate = itemContainer.ContentTemplate;
if (dataTemplate != null)
{
itemContainer.ApplyTemplate(); //有时候会报template没应用到control的错误。
visualChild = dataTemplate.FindName("NotificationChisel", itemContainer);
if (visualChild != null && visualChild is childType)
{
return visualChild as childType;
}
}
}
return null;
}
对于复杂的items control,如第三方控件的,需要用snoop看下可视树来确定寻找的element.
///<summary>
/// Get all the visual childs in the current item of ItemsControl
///</summary>
///<param name="itemsControl">The items control which the current item belongs to.</param>
///<param name="currentItem">The current item in the items control.</param>
///<param name="visualChildName">The spcified visual child's name</param>
///<returns>Return all the visual childs in the current item.</returns>
private List<childType> GetVisualChildrenInDataTemplate<childType>(ItemsControl itemsControl, object currentItem, string visualChildName)
{
List<childType> visualChilds = new List<childType>();
DependencyObject itemContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(currentItem);
if (itemContainer != null)
{
object visualChild = null;
foreach (ContentPresenter conentPresenter in WPFUtils.GetVisualChildrenOfType<ContentPresenter>(itemContainer))
{
DataTemplate dataTemplate = conentPresenter.ContentTemplate;
if (dataTemplate != null)
{
visualChild = dataTemplate.FindName(visualChildName, conentPresenter);
if (visualChild != null && visualChild is childType)
{
visualChilds.Add((childType)visualChild);
}
}
}
}
return visualChilds;
}
///<summary>
/// Get all the childs with the given type within the curent parent.
///</summary>
///<typeparam name="childType">the type of child which need to find.</typeparam>
///<param name="parent">the relative parent object</param>
///<returns>the returned child collection</returns>
public static List<childType> GetVisualChildrenOfType<childType>(DependencyObject parent) where childType : DependencyObject
{
List<childType> childList = new List<childType>();
FindVisualChilds(parent, childList);
return childList;
}
private static void FindVisualChilds<childType>(DependencyObject parent, List<childType> childs) where childType : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child == null)
{
break;
}
if (child is childType)
{
if (childs == null)
{
childs = new List<childType>();
}
childs.Add((childType)child);
}
else
{
FindVisualChilds<childType>(child, childs);
}
}
}