我正在开发一个WPF应用程序。
主窗口的子控件包含在一个网格中。
底部行包含状态栏。
该应用程序必须通知用户。
我想以编程方式在主窗口右下角的用户控件中显示通知。
我希望通知用户控件显示在状态栏和上一行的控件之上。
如何在包含在网格中的其他控件之上显示控件?
任何帮助都将不胜感激
网格具有名为ZIndex的属性。看看这个例子:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="ZIndex Sample">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.ZIndex="3" Fill="blue"/>
<Rectangle Grid.Row="0" Grid.ZIndex="1" Fill="yellow"/>
<Rectangle Grid.Row="0" Grid.ZIndex="2" Fill="green"/>
<!-- Reverse the order to illustrate z-index property -->
<Rectangle Grid.Row="1" Grid.ZIndex="1" Fill="green"/>
<Rectangle Grid.Row="1" Grid.ZIndex="3" Fill="yellow"/>
<Rectangle Grid.Row="1" Grid.ZIndex="2" Fill="blue"/>
</Grid>
如果您没有指定 ZIndex,则面板的子项将按照它们的指定顺序呈现(即最后一个在顶部)。<Expander Name="expander_options" Header="Options" Margin="330,10,0,182" Panel.ZIndex="1">
<StackPanel Margin="10,4,0,0" Panel.ZIndex="2" Background="Aqua">
<CheckBox Margin="4" Content="Option 1" />
<CheckBox Margin="4" Content="Option 2" />
<CheckBox Margin="4" Content="Option 3" />
</StackPanel>
</Expander>
或者,您可以利用Popup(始终位于其他控件的顶部)。并在需要显示更新时切换其IsOpen状态。
<Popup IsOpen="True">
<TextBlock Text="Important update!!" Background="White" Foreground="Black"/>
</Popup>
您可以通过 Dockpanel、stackpanel(右对齐)或 Grid 等多种方式将弹出窗口固定在右下方位置。以下是 Grid 的示例:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2">
<!--Place any control here-->
</StackPanel>
<StackPanel x:Name="BottomRightPanel" Grid.Row="1" Grid.Column="1">
<Popup IsOpen="True" Placement="">
<TextBlock Text="Important update!!" Background="White" Foreground="Black"></TextBlock>
</Popup>
</StackPanel>
</Grid>
HorizontalAlignment="Right"
和 VerticalAlignment="Bottom"
。 - VishalPopup
会创建一个独立的 Win32 窗口,而不是在当前窗口中创建一个覆盖层。这并不是我想要的,因为弹出窗口不会跟随主窗口移动。 - Dai