9

I might be misunderstanding the concept of adorner layers in WPF but I've managed to add a TextBlock to a StackPanel's adorner layer.

How would I clear an adorner layer? So the StackPanel no longer has a TextBlock in it's adorner layer for example?

gideon
  • 19,329
  • 11
  • 72
  • 113

2 Answers2

2

Old question, but I just had the same problem:

AdornerLayers are shared by multiple UIElements. To access the AdornerLayer of any ui-element (of any Visual, to be more specific), you can call static AdornerLayer.GetAdornerLayer(visual). This method walks up the VisualTree, finds the first AdornerDecorator (a container to hold an AdornerLayer) and returns the AdornerLayer associated with the UIElement. Then you can access the Adorners related ot the UIElement, inside that AdornerLayer.

Here's the code to remove all Adorners, related to your StackPanel, from the AdornerLayer that is used by the StackPanel:

var adornerLayer = AdornerLayer.GetAdornerLayer(yourStackPanel);
var adornersOfStackPanel = adornerLayer.GetAdorners(yourStackPanel);

foreach (var adorner in adornersOfStackPanel)
    adornerLayer.Remove(adorner);
m e
  • 71
  • 4
-2

no, you must add or remove the addorned layer

https://github.com/TheCamel/ArchX/blob/master/ArchX.Controls/Ruler/Ruler.cs

if (after)
    layer.Add(GuideAdorner);
else
    layer.Remove(GuideAdorner);
Zoe
  • 27,060
  • 21
  • 118
  • 148
GCamel
  • 612
  • 4
  • 8