简体   繁体   English

如何将UserControl放在前台?

[英]How to place an UserControl in foreground?

I need to display a UserControl over the screen, o top of everything. 我需要在屏幕上显示UserControl,或者所有内容都应位于顶部。

With the following code UserControl show content under ContentGrid. 使用以下代码UserControl在ContentGrid下显示内容。

How do I place UserControl in foreground? 如何将UserControl放在前台?


Main.xaml Main.xaml

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

ZIndex Z索引

http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx http://msdn.microsoft.com/zh-CN/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. 在我的UserControl上,解决了我的问题。

There are different ways to achieve what you're after. 有多种方式可以实现您的追求。 One way would be to set the Panel.ZIndex attached property on it: 一种方法是在其上设置Panel.ZIndex附加属性

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

Another is just to declare it underneath other controls on the XAML page: 另一个只是在XAML页面上的其他控件下声明它:

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

A third, perhaps more suitable method is to use the Adorner Class . 第三种,也许更合适的方法是使用Adorner Class The Adorner Layer is a visual layer above the normal layer where UI elements are displayed. Adorner层是显示UI元素的普通层之上的可视层。 While it is not normally used to display whole UserControl s, it is possible. 虽然它通常不用于显示整个UserControl s时, 可能的。 Please see the answer to the WPF adorner with controls inside question here on Stack Overflow for further information on this. 有关此方面的更多信息,请参见此处的Stack Overflow中有关WPF装饰器的答案,其中包含控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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