public static T FindChildInTemplate<T>(DependencyObject d, Control parent) where T : FrameworkElement { int count = VisualTreeHelper.GetChildrenCount(d); for (int i = 0; i < count; i++) { DependencyObject child = VisualTreeHelper.GetChild(d, i); T t = child as T; if (t != null) { if (t.TemplatedParent == parent) { return t; } } t = FindChildInTemplate<T>(child, parent); if (t != null) { return t; } } return null; }
/// <summary> /// Checks if the manipulation should cause a tilt, and if so starts the tilt effect /// </summary> /// <param name="source">The source of the manipulation (the tilt container, eg entire page)</param> /// <param name="e">The args from the ManipulationStarted event</param> static void TryStartTiltEffect(FrameworkElement source, ManipulationStartedEventArgs e) { foreach (FrameworkElement ancestor in TreeHelpers.GetVisualAncestors((e.OriginalSource as FrameworkElement))) { foreach (Type t in TiltableItems) { if (t.IsAssignableFrom(ancestor.GetType())) { if ((bool)ancestor.GetValue(SuppressTiltProperty) != true) { // Use first child of the control, so that we can add transforms and not // impact any transforms on the control itself FrameworkElement element = VisualTreeHelper.GetChild(ancestor, 0) as FrameworkElement; FrameworkElement container = e.ManipulationContainer as FrameworkElement; if (element == null || container == null) { return; } // touch point relative to the element being tilted Point tiltTouchPoint = container.TransformToVisual(element).Transform(e.ManipulationOrigin); // center of the element being tilted Point elementCenter = new Point(element.ActualWidth / 2, element.ActualHeight / 2); // Camera adjustment Point centerToCenterDelta = GetCenterToCenterDelta(element, source); BeginTiltEffect(element, tiltTouchPoint, elementCenter, centerToCenterDelta); return; } } } } }
public static TFrameElement GetContentPresenter <TFrameElement>(this FrameworkElement control) where TFrameElement : FrameworkElement { var num = VisualTreeHelper.GetChildrenCount(control); if (num == 0) { return(null); } for (int index = 0; index < num; index++) { var obj = VisualTreeHelper.GetChild(control, index); if (obj == null) { continue; } var v = (Visual)obj; if (v is TFrameElement frameElement) { return(frameElement); } if (obj is FrameworkElement fe) { var t = fe.GetContentPresenter <TFrameElement>(); if (t != null) { return(t); } } } return(null); }
public static T GetVisualChildByName <T>(this FrameworkElement root, string name) where T : FrameworkElement { var chil = VisualTreeHelper.GetChild(root, 0); FrameworkElement child = null; int count = VisualTreeHelper.GetChildrenCount(root); for (int i = 0; i < count && child == null; i++) { var current = (FrameworkElement)VisualTreeHelper.GetChild(root, i); if (current != null && current.Name != null && current.Name == name) { child = current; break; } else { child = current.GetVisualChildByName <FrameworkElement>(name); } } return(child as T); }
public static T GetParentOfType <T>(this DependencyObject element) where T : DependencyObject { Type type = typeof(T); if (element == null) { return(null); } DependencyObject parent = VisualTreeHelper.GetParent(element); if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) { parent = ((FrameworkElement)element).Parent; } if (parent == null) { return(null); } else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) { return(parent as T); } return(GetParentOfType <T>(parent)); }
/// <summary> /// True, if node is derived from reference /// </summary> internal static bool IsDescendant(Visual reference, Visual node) { bool success = false; Visual curr = node; while (curr != null) { if (curr == reference) { success = true; break; } // Try to jump up logical links if possible. FrameworkElement logicalCurrent = curr as FrameworkElement; Visual logicalCurrentVisualParent = null; // Check for logical parent and make sure it's a Visual if (logicalCurrent != null) { logicalCurrentVisualParent = logicalCurrent.Parent as Visual; } if (logicalCurrentVisualParent != null) { curr = logicalCurrentVisualParent; } else { // Logical link isn't there; use child link curr = VisualTreeHelper.GetParent(curr) as Visual; } } return(success); }
/// <summary> /// This method is an alternative to WPF's /// <see cref="VisualTreeHelper.GetParent"/> method, which also /// supports content elements. Do note, that for content element, /// this method falls back to the logical tree of the element. /// </summary> /// <param name="child">The item to be processed.</param> /// <returns>The submitted item's parent, if available. Otherwise /// null.</returns> public static DependencyObject GetParentObject(DependencyObject child) { if (child == null) { return(null); } ContentElement contentElement = child as ContentElement; if (contentElement != null) { DependencyObject parent = ContentOperations.GetParent(contentElement); if (parent != null) { return(parent); } FrameworkContentElement fce = contentElement as FrameworkContentElement; return(fce != null ? fce.Parent : null); } ////if it's not a ContentElement, rely on VisualTreeHelper return(VisualTreeHelper.GetParent(child)); }
public static T GetVisualChild <T>(DependencyObject referenceVisual) where T : Visual { Visual child = null; int childCount = VisualTreeHelper.GetChildrenCount(referenceVisual); for (int i = 0; i < childCount; i++) { child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual; if (child != null && child is T) { break; } else if (child != null) { child = GetVisualChild <T>(child); if (child != null && child is T) { break; } } } return(child as T); }
private void FindAllDeferableChildren(DependencyObject reference, List <IDeferableScrollChanged> list) { if (reference == null) { return; } var childCount = VisualTreeHelper.GetChildrenCount(reference); for (int i = 0; i < childCount; i++) { var child = VisualTreeHelper.GetChild(reference, i); var deferable = child as IDeferableScrollChanged; if (deferable != null) { list.Add(deferable); } else { this.FindAllDeferableChildren(child, list); } } }
/// <summary> /// Find a special type child of a root /// </summary> /// <typeparam name="T"></typeparam> /// <param name="root"></param> /// <returns></returns> public static T FindChildOfType <T>(DependencyObject root) where T : class { //创建一个队列结构来存放可视化树的对象 var queue = new Queue <DependencyObject>(); queue.Enqueue(root); //循环查找类型 while (queue.Count > 0) { DependencyObject current = queue.Dequeue(); //查找子节点的对象类型 for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--) { var child = VisualTreeHelper.GetChild(current, i); var typedChild = child as T; if (typedChild != null) { return(typedChild); } queue.Enqueue(child); } } return(null); }
/// <summary> /// Finds a descendent of the element passed as a parameter that has the /// name passed as a parameter. /// </summary> /// <param name="element">The element where we want to start the search.</param> /// <param name="name">The name of the element we're looking for.</param> /// <returns>An element descendent of the element passed as a parameter, with the name /// passed as a parameter.</returns> public static FrameworkElement FindDescendent(FrameworkElement element, string name) { Stack <FrameworkElement> stack = new Stack <FrameworkElement>(); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { stack.Push(VisualTreeHelper.GetChild(element, i) as FrameworkElement); } while (stack.Count > 0) { FrameworkElement poppedElement = stack.Pop(); if (poppedElement != null && String.Equals(poppedElement.Name, name)) { return(poppedElement); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(poppedElement); i++) { stack.Push(VisualTreeHelper.GetChild(poppedElement, i) as FrameworkElement); } } return(null); }
private void btnUpdateTerm_Click(object sender, RoutedEventArgs e) { DataGridRow dgr = null; var visParent = VisualTreeHelper.GetParent(e.OriginalSource as FrameworkElement); while (dgr == null && visParent != null) { dgr = visParent as DataGridRow; visParent = VisualTreeHelper.GetParent(visParent); } if (dgr == null) { return; } var rowIdx = dgr.GetIndex(); term termToUpdate = (term)termDatagrid.Items.GetItemAt(rowIdx); atbHallSearch.SelectedItem = termToUpdate.hall.name; calendar.SelectedDate = termToUpdate.rental_date; txtCalendar.Text = termToUpdate.rental_date.ToShortDateString(); tpTimeFrom.Text = termToUpdate.rent_time_start.ToString(@"hh\:mm"); tpTimeUntil.Text = termToUpdate.rent_time_end.ToString(@"hh\:mm"); itemDatagrid.Items.Clear(); foreach (item_list i in termToUpdate.items) { itemDatagrid.Items.Add(i); } notes.Text = termToUpdate.note; ChangeTerm = true; termIndex = rowIdx; expander.IsExpanded = true; }
private void btnUpdateItem_Click(object sender, RoutedEventArgs e) { DataGridRow dgr = null; var visParent = VisualTreeHelper.GetParent(e.OriginalSource as FrameworkElement); while (dgr == null && visParent != null) { dgr = visParent as DataGridRow; visParent = VisualTreeHelper.GetParent(visParent); } if (dgr == null) { return; } var rowIdx = dgr.GetIndex(); item_list itemToUpdate = (item_list)itemDatagrid.Items.GetItemAt(rowIdx); atbItemSearch.SelectedItem = itemToUpdate.item; tbItemSearch.Text = itemToUpdate.item.name; tbItemCount.Text = itemToUpdate.quantity.ToString(); ChangeItem = true; }
private static void OnFocusVisualIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { var focusVisual = (Control)sender; if ((bool)e.NewValue) { if ((VisualTreeHelper.GetParent(focusVisual) as Adorner)?.AdornedElement is FrameworkElement focusedElement) { SetIsSystemFocusVisualVisible(focusedElement, true); focusVisual.Margin = FocusVisualHelper.GetFocusVisualMargin(focusedElement); SetFocusedElement(focusVisual, focusedElement); } } else { FrameworkElement focusedElement = GetFocusedElement(focusVisual); if (focusedElement != null) { focusedElement.ClearValue(IsSystemFocusVisualVisiblePropertyKey); focusVisual.ClearValue(FrameworkElement.MarginProperty); focusVisual.ClearValue(FocusedElementProperty); } } }
public static T VisualDescendant <T>(this Visual element) where T : class { if (element == null) { return(default(T)); } if (element.GetType() == typeof(T)) { return(element as T); } T foundElement = null; (element as FrameworkElement)?.ApplyTemplate(); for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { var visual = VisualTreeHelper.GetChild(element, i) as Visual; foundElement = visual.VisualDescendant <T>(); if (foundElement != null) { break; } } return(foundElement); }
/// <summary> /// Eventos para el manejo del renglon de detalle /// </summary> /// <param name="parent"></param> /// <param name="name"></param> /// <returns></returns> public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name) { int childnum = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childnum; i++) { var child = VisualTreeHelper.GetChild(parent, i); if (child is FrameworkElement && ((FrameworkElement)child).Name == name) { return(child as FrameworkElement); } else { var s = GetTemplateChildByName(child, name); if (s != null) { return(s); } } } return(null); }
/// <summary> /// Gets the first parent, parent's parent, etc. from the /// visual tree that matches the specified type /// </summary> public static T GetAntecedent <T>(this DependencyObject root) where T : UIElement { if (root == null) { return(null); } if (root is T) { return((T)root); } else { DependencyObject parent = VisualTreeHelper.GetParent(root); if (parent == null) { return(null); } else { return(parent.GetAntecedent <T>()); } } }
public IInputElement InputHitTest(Point point) { Rect bounds = new Rect(this.RenderSize); if (bounds.Contains(point)) { foreach (UIElement child in VisualTreeHelper.GetChildren(this).OfType <UIElement>()) { Point offsetPoint = point - child.VisualOffset; IInputElement hit = child.InputHitTest(offsetPoint); if (hit != null) { return(hit); } } return(this); } else { return(null); } }
/// <summary> /// Find a specific parent object type in the visual tree /// </summary> public static T FindParentControl <T>(DependencyObject outerDepObj) where T : DependencyObject { DependencyObject dObj = VisualTreeHelper.GetParent(outerDepObj); if (dObj == null) { return(null); } if (dObj is T) { return(dObj as T); } while ((dObj = VisualTreeHelper.GetParent(dObj)) != null) { if (dObj is T) { return(dObj as T); } } return(null); }
public static void FilterINameableListBox(this FrameworkElement element, string filter) { for (int i = 0; i != VisualTreeHelper.GetChildrenCount(element); i += 1) { FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement; if (child != null) { if (child is ListBoxItem) { child.Visibility = Visibility.Visible; if (filter != "") { INameable ctx = (INameable)child.DataContext; if (!ctx.Name.ToLower().Contains(filter.ToLower())) child.Visibility = Visibility.Collapsed; } } else { child.FilterINameableListBox(filter); } } } }
public void RenderedGeometryInWindowFillStroke() { Window w = new Window(); Ellipse e = new Ellipse(); e.Fill = Brushes.Red; e.Stroke = Brushes.Blue; e.Width = 100; e.Height = 100; w.Content = e; w.Show(); EllipseGeometry rendered_geometry = (EllipseGeometry)e.RenderedGeometry; Assert.AreEqual(rendered_geometry.Bounds, new Rect(0.5, 0.5, 99, 99), "1"); DrawingGroup drawing_group = VisualTreeHelper.GetDrawing(e); Assert.AreEqual(drawing_group.Children.Count, 1, "2"); EllipseGeometry drawing_geometry = (EllipseGeometry)((GeometryDrawing)drawing_group.Children [0]).Geometry; Assert.AreNotSame(rendered_geometry, drawing_geometry, "3"); Assert.AreEqual(drawing_geometry.Bounds, new Rect(0.5, 0.5, 99, 99), "4"); }
// Methods public static FrameworkElement GetChildByName(this FrameworkElement parentVisual, string partName) { var childByName = parentVisual.FindName(partName) as FrameworkElement; if (childByName != null) { return(childByName); } int childrenCount = VisualTreeHelper.GetChildrenCount(parentVisual); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parentVisual, i) as FrameworkElement; if (child != null) { childByName = child.GetChildByName(partName); if (childByName != null) { return(childByName); } } } return(null); }
private static void FindVisualChildren <T>(Func <FrameworkElement, bool> predicate, FrameworkElement element, bool skipChildren, List <T> list) where T : FrameworkElement { if (element != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { var child = VisualTreeHelper.GetChild(element, i) as T; if (child != null && predicate(child)) { list.Add(child); } else if (skipChildren) { continue; } if (child != null) { FindVisualChildren(predicate, child, skipChildren, list); } } } }
private void ListViewDeck_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { if (_newDeck == null) { return; } var originalSource = (DependencyObject)e.OriginalSource; while ((originalSource != null) && !(originalSource is ListViewItem)) { originalSource = VisualTreeHelper.GetParent(originalSource); } if (originalSource != null) { var card = (Card)ListViewDeck.SelectedItem; if (card == null) { return; } AddCardToDeck((Card)card.Clone()); _newDeckUnsavedChanges = true; } }
public static 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); } } } return(null); }
public static IEnumerable<DependencyObject> GetParents(this DependencyObject child) { while (child != null) { var parent = LogicalTreeHelper.GetParent(child); if (parent == null) { if (child is FrameworkElement) { parent = VisualTreeHelper.GetParent(child); } if (parent == null && child is ContentElement) { parent = ContentOperations.GetParent((ContentElement)child); } if (parent == null) { parent = InheritanceContextProp.GetValue(child, null) as DependencyObject; } } child = parent; yield return parent; } }
private static ScrollViewer GetScrollViewer(DependencyObject element) { if (element is ScrollViewer) { return((ScrollViewer)element); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { var child = VisualTreeHelper.GetChild(element, i); var result = GetScrollViewer(child); if (result == null) { continue; } else { return(result); } } return(null); }
/// <summary> /// This gives the focus to the indicated control. If it is not focusable, walk the visual tree until a control that can accept it is found. /// </summary> /// <param name="control">The control.</param> /// <returns> /// True if a control accepted the focus. /// </returns> /// <remarks> /// This uses a depth-first search. /// </remarks> public static bool FocusRecursive(this DependencyObject control) { var focusable = control as UIElement; //Try to give the focus to the control. Note that this will fail if the control is hidden. if (focusable != null && focusable.Focusable && focusable.Focus()) { return(true); } var childCount = VisualTreeHelper.GetChildrenCount(control); for (var i = 0; i < childCount; i++) { var child = VisualTreeHelper.GetChild(control, i); //Check to see if any of the child objects can accept the focus if (FocusRecursive(child)) { return(true); } } return(false); //didn't find a focusable control }
/// <summary> /// Finds a child element of the provided DependencyObject of the specified type. /// </summary> /// <typeparam name="T">The type of the child object you would like to find.</typeparam> /// <param name="element">The parent DependencyObject to search through.</param> /// <returns>A child element, of specified type, from the provided DependencyObject. /// Null if no child of specified type is found.</returns> internal static T FindChild <T>(DependencyObject element) where T : class { var els = new List <DependencyObject> { element }; while (els.Count != 0) { var child = els[0]; els.RemoveAt(0); var typedChild = child as T; if (null != typedChild) { return(typedChild); } for (var i = 0; i < VisualTreeHelper.GetChildrenCount(child); i++) { els.Add(VisualTreeHelper.GetChild(child, i)); } } return(null); }
/// <summary> /// Finds a child of the current object in the visual tree. /// </summary> /// <typeparam name="TChild">The type of the requested child object.</typeparam> /// <param name="source">The object in which to find the child.</param> /// <param name="childName">The element name of the requested child.</param> /// <returns>The found child object, or null.</returns> public static TChild FindVisualChild <TChild>(this DependencyObject source, string childName) where TChild : DependencyObject { if (source == null) { throw new ArgumentNullException("source"); } TChild foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(source); for (int i = 0; i < childrenCount && foundChild == null; i++) { var child = VisualTreeHelper.GetChild(source, i); if (child.GetType() != typeof(TChild)) { // Not the requested type, recursively drill down the tree foundChild = FindVisualChild <TChild>(child, childName); } else if (!string.IsNullOrEmpty(childName)) { // Compare the requested child name var frameworkElement = child as FrameworkElement; if (frameworkElement != null && frameworkElement.Name == childName) { foundChild = (TChild)child; } } else { // Type matches, no name requested foundChild = (TChild)child; } } return(foundChild); }