【发布时间】:2014-06-12 12:16:51
【问题描述】:
我正在开发 WPF 应用程序。
主窗口的子控件包含在一个网格中。
底行包含一个状态栏。
应用程序必须通知用户。
我想以编程方式在主窗口右下角的用户控件中显示通知。
我希望通知用户控件显示在状态栏和上一行中的控件上。
如何在网格中包含的其他控件上显示控件?
任何帮助将不胜感激
【问题讨论】:
我正在开发 WPF 应用程序。
主窗口的子控件包含在一个网格中。
底行包含一个状态栏。
应用程序必须通知用户。
我想以编程方式在主窗口右下角的用户控件中显示通知。
我希望通知用户控件显示在状态栏和上一行中的控件上。
如何在网格中包含的其他控件上显示控件?
任何帮助将不胜感激
【问题讨论】:
Grid 有一个名为 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,则面板的子项将按照指定的顺序呈现(即顶部的最后一个)。
【讨论】:
我已经解决了我的问题。
我已修改主窗口的 XAML 标记。
我已经在新网格的同一单元格中声明了通知用户控件和包含主窗口子控件的网格。
我在通知用户控件的开始标签中设置了一些属性:
当通知用户控件可见时:
【讨论】:
试试这个,
使用 Panel.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"></TextBlock>
</Popup>
有几种方法可以将弹出窗口粘贴到右下位置 - Dockpanel、stackpanel(右对齐)、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"。
Popup 在当前窗口中创建一个单独的 Win32 窗口而不是覆盖,这不是我想要的,因为弹出窗口不跟随主窗口。