Capture Changes to Parent Visual Tree
Asked Answered
S

0

2

From within a WPF FrameworkElement, how can I capture any changes to the visual tree path from the root to the FrameworkElement? i.e. when the FrameworkElement, or any of its parents, is added to, removed from, or moved to another branch of a visual tree?

  • I have been looking for appropriate events, but while FrameworkElement has an OnVisualParentChanged method, it does not offer an according event that handlers could be registered for without subclassing the framework element. Hence, this cannot be applied to arbitrary, already instantiated FrameworkElements.
  • In theory, A RelativeSource binding should help me here, but for that to work, I already need to know which parent node (what type, what level) I am looking for, which is not the case (see below).

Use case: For this other question, one promising possible solution would involve using the current NameScope relevant for a FrameworkElement. The appropriate NameScope instance can be retrieved from the NameScope attached property, but it is not set for every node in the tree, only for roots of visual trees constructed from control templates and the window.

Therefore, to find the appropriate NameScope for my FrameworkElement, I need to walk up the visual tree until I find a node that has a non-null value set for the NameScope attached property, and I will have to redo this to possible update the NameScope every time anything in the visual tree above my FrameworkElement has changed.

This discussion touches upon the topic, but it is already 6 years old and doesn't have a solution.

Shinn answered 2/7, 2015 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.