简体   繁体   中英

How to place an UserControl in foreground?

I need to display a UserControl over the screen, o top of everything.

With the following code UserControl show content under ContentGrid.

How do I place UserControl in foreground?


Main.xaml

<Grid x:Name="LayoutRoot">
    <local:AdvertisementsFullScreen>
    </local:AdvertisementsFullScreen>

ZIndex

http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx

<Grid>
<Grid Panel.ZIndex="1">
    <Rectangle Fill="Blue" Width="50" Height="50" Panel.ZIndex="1" />
    <Rectangle Fill="Red" Width="50" Height="50" Panel.ZIndex="0" />
</Grid>
<Grid Panel.ZIndex="0">
    <Rectangle Fill="Green" Width="50" Height="50" Panel.ZIndex="0" />
    <Rectangle Fill="Yellow" Width="50" Height="50" Panel.ZIndex="0" />
</Grid>

Using

<UserControl Panel.ZIndex="100" />

On my UserControl, solved my problem.

There are different ways to achieve what you're after. One way would be to set the Panel.ZIndex attached property on it:

<local:AdvertisementsFullScreen Panel.ZIndex="10" />

Another is just to declare it underneath other controls on the XAML page:

<local:AdvertisementsFullScreen /> 
<local:AdvertisementsFullScreen /> <!-- This one is on top -->

A third, perhaps more suitable method is to use the Adorner Class . The Adorner Layer is a visual layer above the normal layer where UI elements are displayed. While it is not normally used to display whole UserControl s, it is possible. Please see the answer to the WPF adorner with controls inside question here on Stack Overflow for further information on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM