2011-03-27 108 views

回答

120

如果您使用在布局一个帆布或网格,给控制要在上面提出了更高的zIndex的

MSDN

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="ZIndex Sample"> 
    <Canvas> 
    <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="100" Canvas.Left="100" Fill="blue"/> 
    <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="150" Canvas.Left="150" Fill="yellow"/> 
    <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="200" Canvas.Left="200" Fill="green"/> 

    <!-- Reverse the order to illustrate z-index property --> 

    <Rectangle Canvas.ZIndex="1" Width="100" Height="100" Canvas.Top="300" Canvas.Left="200" Fill="green"/> 
    <Rectangle Canvas.ZIndex="3" Width="100" Height="100" Canvas.Top="350" Canvas.Left="150" Fill="yellow"/> 
    <Rectangle Canvas.ZIndex="2" Width="100" Height="100" Canvas.Top="400" Canvas.Left="100" Fill="blue"/> 
    </Canvas> 
</Page> 

如果不指定Z-索引时,面板的孩子按照他们指定的顺序呈现(即最后一个在顶部)。

如果你想要做更复杂的事情,你可以看看如何在Silverlight中实现ChildWindow。它覆盖半透明背景并弹出整个RootVisual。

35

网格的同一单元格中的控件将被反向渲染。因此,将一个控件放在另一个控件上的简单方法是将它放在同一个单元格中。

下面是一个有用的例子,它弹出一个面板,在执行长时间运行的任务时(即,BusyMessage绑定的属性不为空),在视图中(即用户控件)禁用所有内容, :

<Grid> 

    <local:MyUserControl DataContext="{Binding}"/> 

    <Grid> 
     <Grid.Style> 
      <Style TargetType="Grid"> 
       <Setter Property="Visibility" 
         Value="Visible" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding BusyMessage}" 
           Value="{x:Null}"> 
         <Setter Property="Visibility" 
           Value="Collapsed" /> 
        </DataTrigger> 

       </Style.Triggers> 
      </Style> 
     </Grid.Style> 
     <Border HorizontalAlignment="Stretch" 
       VerticalAlignment="Stretch" 
       Background="DarkGray" 
       Opacity=".7" /> 
     <Border HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Background="White" 
       Padding="20" 
       BorderBrush="Orange" 
       BorderThickness="4"> 
      <TextBlock Text="{Binding BusyMessage}" /> 
     </Border> 
    </Grid> 
</Grid> 
8

这是WPF中Adorners的常见功能。装饰者通常出现在所有其他控件之上,但其他提到z顺序的答案可能更适合您的情况。

49

Robert Rossney有很好的解决方案。这是我过去使用的另一种解决方案,将“叠加”与其他内容分开。该解决方案利用所附属性Panel.ZIndex将“覆盖”放在其他任何位置上。您可以在代码中设置“叠加层”的可见性,也可以使用DataTrigger

<Grid x:Name="LayoutRoot"> 

<Grid x:Name="Overlay" Panel.ZIndex="1000" Visibility="Collapsed"> 
    <Grid.Background> 
     <SolidColorBrush Color="Black" Opacity=".5"/> 
    </Grid.Background> 

    <!-- Add controls as needed --> 
    </Grid> 

    <!-- Use whatever layout you need --> 
    <ContentControl x:Name="MainContent" /> 

</Grid> 
+0

作品!但我如何将Overlay Grid移动到正确位置? – 2012-04-01 11:53:25

+0

这个覆盖图将覆盖整个窗口,而不仅仅是一个特定的区域。 – 2012-04-02 03:02:21

+0

有史以来最好的解决方案!谢谢! – 2017-10-27 05:44:11

3
<Canvas Panel.ZIndex="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="570"> 
    <!-- YOUR XAML CODE --> 
</Canvas> 
14

将要带给前在您的XAML代码末端的控制。即

<Grid> 
    <TabControl ...> 
    </TabControl> 
    <Button Content="ALways on top of TabControl Button"/> 
</Grid>