Why AdornerLayers are always the top most layers? Is there a way to change it?

喜欢而已 提交于 2019-12-06 07:28:18

问题


  1. Why is that the adorner layer is always rendered as the top most layer in an application (under AdornerDecorator - refer screenshot)?
  2. Is there a way to change the layer/level on to which the adorners can be drawn?

In the following screenshot, AdornerLayer is added to AdornerDecorator and the Adorners (MyAdorners) are added to this AdornerLayer. But the AdornerLayer is retrieved like this,

        AdornerLayer layer1 = AdornerLayer.GetAdornerLayer(button1);
        layer1.Add(new MyAdorner(button1));


回答1:


To answer my second question,

Is there a way to change the layer/level on to which the adorners can be drawn?

I guess I have found a solution. Just place an AdornerDecorator element around the level on to which the adorners needs to be rendered. Any control requiring an adorner layer would use this AdornerDecorator element to place its decorators.

Here I have moved the adorners to a different level using the following code snippet.

<Window x:Class="CustomAdornerLayer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <StackPanel Background="Yellow" Width="Auto">
        <Button>Button3</Button>
    </StackPanel>
    <AdornerDecorator>
        <Grid>
            <AdornerDecorator>
                <Button x:Name="button1" Margin="70,73,265,158">Button1</Button>
            </AdornerDecorator>
            <AdornerDecorator>
                <Button x:Name="button2" Margin="87,51,248,180">Button2</Button>
            </AdornerDecorator>
        </Grid>
    </AdornerDecorator>
</Grid>

While the AdornerLayer is still queried in the same way,

        AdornerLayer layer1 = AdornerLayer.GetAdornerLayer(button1);
        layer1.Add(new MyAdorner(button1));
        AdornerLayer layer2 = AdornerLayer.GetAdornerLayer(button2);
        layer2.Add(new MyAdorner(button2));

Kindly correct me if I am wrong.




回答2:


Adorner layer is provided by AdornerDecorator. When you ask for the layer for the given control wpf looks for the AdornerDecorator upper visual tree. Why do you need to change this logic? Adorners system made intentionally this way for decorations to appear above the decorated element.

You can search for AdornerDecorator yourself with VisualTreeHelper



来源:https://stackoverflow.com/questions/13209695/why-adornerlayers-are-always-the-top-most-layers-is-there-a-way-to-change-it

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