如何设置将覆盖WPF中其他控件的弹出式面板索引?

时间:2017-03-03 00:26:39

标签: c# wpf xaml

我有一个关于如何在网格中正确设置Popup的Panel.ZIndex的问题。目标是当我单击紧急按钮时,弹出窗口将显示一个图像并覆盖(覆盖)按钮(见下面的屏幕截图)。

我已将网格Panel.ZIndex="0"和弹出窗口设置为Panel.ZIndex="1"但是,弹出窗口不会覆盖按钮。

这是XAML实现。

<StackPanel Background="Black">
    <Grid Background="#253341" Panel.ZIndex="0">
        <Popup HorizontalOffset="-5" VerticalOffset="0" IsOpen="False" 
               HorizontalAlignment="Left" VerticalAlignment="Top"
               Name="EmergencyPopup" Placement="RelativePoint" AllowsTransparency="True"
               PlacementTarget="{Binding ElementName=EmergencyButton}"
               Width="1080" Height="1920" Panel.ZIndex="1">
            <Border BorderBrush="Black" BorderThickness="2" CornerRadius="2">
                <Grid>
                    <Image Source="{StaticResource EdenParkInfoImg}"/>
                    <Label FontWeight="Bold" Foreground="Red" HorizontalAlignment="Right" FontSize="25"
                           MouseLeftButtonDown="Label_MouseLeftButtonDown">close X</Label>
                </Grid>
            </Border>
        </Popup>
    </Grid>
</StackPanel>

弹出窗口和按钮的屏幕截图(红色框表示Popup中的图像位于按钮后面) enter image description here

我是否正确设置了网格和弹出窗口的Panel.ZIndex?这样做的正确方法是什么?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我确实找到了解决问题的方法。在我的弹出对话框xaml实现中,我将Horizo​​ntalOffset设置为0,将VerticalOffset设置为180.这样,Popup对话框垂直覆盖按钮,ZIndex不再重要。 WPFUser是对的,它应该在没有明确定义ZIndex的情况下工作。

<Popup HorizontalOffset="0" VerticalOffset="180" IsOpen="False" Width="1080" 
                       HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="1920"
                       Name="EmergencyPopup" Placement="Top" AllowsTransparency="True">
                    <Border BorderBrush="Black">
                        <Grid>
                        <Image Source="{StaticResource EdenParkInfoImg}" />
                        <Label FontWeight="Bold" Foreground="Red" HorizontalAlignment="Right" FontSize="25"
                               MouseLeftButtonDown="Label_MouseLeftButtonDown" Margin="0,0,15,0">close X
                        </Label>
                        </Grid>
                    </Border>
                </Popup>