GetAdornerLayer mysteriously returning null

会有一股神秘感。 提交于 2019-12-01 03:21:20

The docs for AdornerLayer.GetAdornerLayer specify:

If no adorner layers are found, the method returns null.

So my guess is that there are no adorner layers... do you have any reason to believe that this shouldn't be the case? What guarantee are you currently relying on that there will be an adorner layer in the visual tree?

I know this is an old question but I had this issue today.

In my case I had a class that is based on Window and GetAdornerLayer() returned null. It turned out that the ControlTemplate for my derived class did not contain the AdornerDecorator. Adding that as the top level in the ControlTemplate solved the issue.

<Style TargetType="my:MyWindow" BasedOn="{StaticResource {x:Type Window}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="my:MyWindow">
                <AdornerDecorator>
                <DockPanel ...>
                </DockPanel>
                </AdornerDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I'm curious as to whether or not this was really solved. An AdornerDecorator provides an AdornerLayer for element below it -- and everything will be below it. It is a decorator, meaning it has a Child that is the content. That content is being provided with an AdornerLayer. So, if you put an AdornerDecorator in your XAML and the child is the border, the border does have an AdornerLayer.

Furthermore, Window defines an AdornerDecorator as the top of the visual tree so any element in a Window will have an AdornerLayer above it. So, if your conent above was in a Window...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!