我需要让控件出现在所有其他控件之上,因此它将部分覆盖它们 .
如果您在布局中使用 Canvas 或 Grid ,请将控件放在更高的 ZIndex 上 .
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>
如果未指定 ZIndex ,则面板的子项将按指定的顺序呈现(即最后一个在顶部) .
如果您希望执行更复杂的操作,可以查看如何在Silverlight中实现 ChildWindow . 它覆盖了半透明背景并弹出整个 RootVisual .
ChildWindow
RootVisual
Robert Rossney有一个很好的解决方案 . 这里's an alternative solution I' ve过去用于将"Overlay"与其余内容分开 . 此解决方案利用附加属性 Panel.ZIndex 将"Overlay"放在其他所有内容之上 . 您可以在代码中设置"Overlay"的可见性,也可以使用 DataTrigger .
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>
网格的同一单元格中的控件从后向前呈现 . 因此,将一个控件置于另一个控件之上的简单方法是将其放在同一个单元格中 .
这是一个有用的示例,它弹出一个面板,在执行长时间运行的任务时(即 BusyMessage bound属性不为null时),使用busy消息禁用视图中的所有内容(即用户控件):
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>
将您想要的控件放在xaml代码末尾 . 即
<Grid> <TabControl ...> </TabControl> <Button Content="ALways on top of TabControl Button"/> </Grid>
这是WPF中Adorners的常见功能 . Adorners通常出现在所有其他控件之上,但提及z-order的其他答案可能更适合您的情况 .
<Canvas Panel.ZIndex="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="570"> <!-- YOUR XAML CODE --> </Canvas>
6 回答
如果您在布局中使用
Canvas
或Grid
,请将控件放在更高的ZIndex
上 .来自MSDN:
如果未指定
ZIndex
,则面板的子项将按指定的顺序呈现(即最后一个在顶部) .如果您希望执行更复杂的操作,可以查看如何在Silverlight中实现
ChildWindow
. 它覆盖了半透明背景并弹出整个RootVisual
.Robert Rossney有一个很好的解决方案 . 这里's an alternative solution I' ve过去用于将"Overlay"与其余内容分开 . 此解决方案利用附加属性
Panel.ZIndex
将"Overlay"放在其他所有内容之上 . 您可以在代码中设置"Overlay"的可见性,也可以使用DataTrigger
.网格的同一单元格中的控件从后向前呈现 . 因此,将一个控件置于另一个控件之上的简单方法是将其放在同一个单元格中 .
这是一个有用的示例,它弹出一个面板,在执行长时间运行的任务时(即
BusyMessage
bound属性不为null时),使用busy消息禁用视图中的所有内容(即用户控件):将您想要的控件放在xaml代码末尾 . 即
这是WPF中Adorners的常见功能 . Adorners通常出现在所有其他控件之上,但提及z-order的其他答案可能更适合您的情况 .