【问题标题】:Displaying a control over other controls in a grid in WPF在 WPF 的网格中显示对其他控件的控件
【发布时间】:2014-06-12 12:16:51
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf layout overlay


    【解决方案1】:

    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,则面板的子项将按照指定的顺序呈现(即顶部的最后一个)。

    【讨论】:

    • ZIndex 实际上是 WPF 中 Panel 上的附加属性,也被其他重叠布局面板(如 Canvas)使用。
    • @JohnBowen 感谢您的信息。我会记住的。
    • @Vishal 我不能将状态栏和通知用户控件放在同一行。通知用户控件的高度将大于状态栏的高度。通知用户控件必须与底行重叠并且部分在上面的控件之上。
    【解决方案2】:

    我已经解决了我的问题。
    我已修改主窗口的 XAML 标记。
    我已经在新网格的同一单元格中声明了通知用户控件和包含主窗口子控件的网格。

    我在通知用户控件的开始标签中设置了一些属性:

    • Grid.ZIndex="1"
    • Horizo​​ntalAlignment="右"
    • VerticalAlignment="底部"
    • Margin="0 0 20 20"

    当通知用户控件可见时:

    • 通知用户控件位于包含主窗口子控件的网格上
    • 通知用户控件位于主窗口的右下角

    【讨论】:

      【解决方案3】:

      试试这个,

      使用 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>
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我已经阅读了关于 Popup 放置的 MSDN 文档,但我还没有找到如何在主窗口的右下角放置 Popup。我该怎么做?
        • @user1139666 尝试在 Popup 上使用 HorizontalAlignment="Right"VerticalAlignment="Bottom"
        • 更新了我上面的答案。
        • 根据我的经验,Popup 在当前窗口中创建一个单独的 Win32 窗口而不是覆盖,这不是我想要的,因为弹出窗口不跟随主窗口。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        • 2012-03-21
        • 2010-12-25
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        相关资源
        最近更新 更多