fos*_*son 152
如果您正在使用Canvas
或Grid
在布局中,请将控件放在更高的位置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>
Run Code Online (Sandbox Code Playgroud)
如果未指定ZIndex
,则面板的子项按指定顺序呈现(即最后一个在顶部).
如果您希望执行更复杂的操作,可以查看ChildWindow
Silverlight中的实现方式.它覆盖了半透明背景和弹出整个背景RootVisual
.
Met*_*urf 66
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>
Run Code Online (Sandbox Code Playgroud)
Rob*_*ney 39
网格的同一单元格中的控件是从后向前呈现的.因此,将一个控件置于另一个控件之上的简单方法是将其放在同一个单元格中.
这是一个有用的示例,它会弹出一个面板,在执行长时间运行的任务时(即BusyMessage
绑定属性不为null时),使用busy消息禁用视图中的所有内容(即用户控件):
<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>
Run Code Online (Sandbox Code Playgroud)
art*_*tos 22
将您想要的控件放在xaml代码末尾.即
<Grid>
<TabControl ...>
</TabControl>
<Button Content="ALways on top of TabControl Button"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)