When does a WPF adorner layer first become available?

Posted by aoven on Stack Overflow See other posts from Stack Overflow or by aoven
Published on 2010-04-16T10:52:46Z Indexed on 2010/04/16 15:23 UTC
Read the original article Hit count: 438

Filed under:
|
|

I'm trying to add an overlay effect to my UserControl and I know that's what adorners are used for in WPF. But I'm a bit confused about how they supposedly work. I figured that adorner layer is implicitly handled by WPF runtime, and as such, should always be available.

But when I create an instance of my UserControl in code, there is no adorner layer there. The following code fails with exception:

var view = new MyUserControl();
var target = view.GetAdornerTarget(); // This returns a specific UI control.
var layer = AdornerLayer.GetAdornerLayer(target);
if (layer == null)
{
    throw new Exception("No adorner layer at the moment.");
}

Can someone please explain to me, how this is supposed to work? Do I need to place the UserControl instance into a top-level Window first? Or do I need to define the layer myself somehow? Digging through documentation got me nowhere.

Thank you!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about adorner