| name | navigating-mewui-tree |
| description | Traverses and manipulates the MewUI visual tree. Use when understanding element hierarchy, finding parent/child elements, implementing IVisualTreeHost, or working with element lifecycle. |
Element Hierarchy
Element (base)
└─ UIElement (input, visibility, focus)
└─ FrameworkElement (sizing, margin, alignment)
├─ Panel (multi-child: StackPanel, Grid, Canvas, DockPanel)
├─ Control (themed elements: Button, Label, TextBox)
│ ├─ ContentControl (single child: Window)
│ └─ Border (decorator)
└─ ...
Parent-Child Relationships
Element? parent = element.Parent;
panel.Add(child);
panel.Remove(child);
panel.Children;
contentControl.Content = child;
border.Child = child;
Tree Traversal
Element? root = element.FindVisualRoot();
bool isChild = element.IsDescendantOf(ancestor);
bool isParent = element.IsAncestorOf(descendant);
static T? FindAncestor<T>(Element element) where T : Element
{
for (var cur = element.Parent; cur != null; cur = cur.Parent)
if (cur is T match) return match;
return null;
}
Note: VisualTree.Visit() exists but is internal - use FindVisualRoot/IsDescendantOf for external code.
IVisualTreeHost (Internal)
Interface for elements with children (internal API):
internal interface IVisualTreeHost
{
void VisitChildren(Action<Element> visitor);
}
void IVisualTreeHost.VisitChildren(Action<Element> visitor)
{
foreach (var child in _children)
visitor(child);
}
void IVisualTreeHost.VisitChildren(Action<Element> visitor)
{
if (Content != null) visitor(Content);
}
Visual Root Lifecycle
protected virtual void OnVisualRootChanged(Element? oldRoot, Element? newRoot)
{
}
Coordinate Transforms
var transform = element.TransformToAncestor(ancestor);
Point translated = element.TranslatePoint(localPoint, otherElement);
protected override void OnMouseMove(MouseEventArgs e)
{
Point pos = e.Position;
}
Hit testing: See hit-testing.md