在WPF中,在网格中显示控件控制其他控件

11

我正在开发一个WPF应用程序。
主窗口的子控件包含在一个网格中。
底部行包含状态栏。

该应用程序必须通知用户。
我想以编程方式在主窗口右下角的用户控件中显示通知。
我希望通知用户控件显示在状态栏和上一行的控件之上。

如何在包含在网格中的其他控件之上显示控件?
任何帮助都将不胜感激

4个回答

10

网格具有名为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,则面板的子项将按照它们的指定顺序呈现(即最后一个在顶部)。

2
ZIndex实际上是WPF中Panel的一个附加属性,也被其他重叠布局面板(如Canvas)使用。 - John Bowen
@JohnBowen 感谢您的信息。我会记在心里的。 - Vishal
@Vishal 我无法将状态栏和通知用户控件放在同一行上。通知用户控件的高度将大于状态栏的高度。通知用户控件必须重叠底部行并部分覆盖上面的控件。 - user1139666

6
我已经解决了我的问题。 我修改了主窗口的XAML标记。 我在一个新的网格中声明了通知用户控件和包含主窗口子控件的网格相同的单元格。
我在通知用户控件的开放标签中设置了一些属性:
- Grid.ZIndex="1" - HorizontalAlignment="Right" - VerticalAlignment="Bottom" - Margin="0 0 20 20"
当通知用户控件可见时:
- 通知用户控件位于包含主窗口子控件的网格之上 - 通知用户控件位于主窗口的右下角

4
尝试这个,
使用Panel.ZIndex可以实现这个。 enter image description here
<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>

4

或者,您可以利用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>

我已经阅读了有关Popup放置的MSDN文档,但我没有找到如何将Popup放置在主窗口的右下角。我该怎么做? - user1139666
@user1139666 尝试在 Popup 上使用 HorizontalAlignment="Right"VerticalAlignment="Bottom" - Vishal
我已经更新了我的回答。 - Manish Basantani
2
根据我的经验,Popup 会创建一个独立的 Win32 窗口,而不是在当前窗口中创建一个覆盖层。这并不是我想要的,因为弹出窗口不会跟随主窗口移动。 - Dai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接