public static void RemoveFromParent(this FrameworkElement elem) { if (elem.Parent == null) { return; } try { ContentX contentProv = new ContentX(elem.Parent); var content = contentProv.Content; if (content is UIElement) { contentProv.Content = null; } if (content is UIElementCollection) { ((UIElementCollection)content).Remove(elem); } else { throw new Exception("unknown parent type"); } } catch (Exception exep) { throw new Exception("can't solve RemoveFromParent", exep); } }
public static IEnumerable <T> GetChildren <T>(this FrameworkElement elem) where T : DependencyObject { ContentX content = new ContentX(elem); if (content.Content != null) { var enumer = content.Content as IEnumerable; if (enumer != null) { foreach (var item in enumer) { if (item is T) { yield return(item as T); } } } else { if (content.Content is T) { yield return(content.Content as T); } } } }