【问题标题】:How to Center a Popup Control on a Page如何在页面上居中弹出控件
【发布时间】:2013-10-12 17:46:07
【问题描述】:

我正在使用以下Popup,默认情况下它位于屏幕的左上角。我希望能够在我的视图中居中,以便我可以将其拉伸到屏幕的宽度和高度并更改不透明度。我希望用户能够在弹出窗口显示在屏幕上时看到它背后的内容,但也让弹出窗口覆盖所有内容。我目前的情况如下

<Grid x:Name="filterGrid">
                <Popup x:Name="trialPopup" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0.5">
                    <Border Margin="3" BorderBrush="{StaticResource PhoneForegroundBrush}">
                            <StackPanel Background="#FF1BA1E2">
                            <TextBlock x:Name="modeTextBlock" HorizontalAlignment="Center"/>
                            <TextBlock Text="Do you wish to purchase the application?" TextWrapping="Wrap" HorizontalAlignment="Center"/>
                                <Grid Margin="0,10" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width=".5*"/>
                                    <ColumnDefinition Width=".5*"/>
                                </Grid.ColumnDefinitions>
                                    <Button x:Name="purchaseButton" Grid.Column="0" Content="purchase" Click="popupButton_Click"/>
                                    <Button x:Name="cancelButton" Grid.Column="1" Content="cancel" Click="popupButton_Click"/>
                                </Grid>
                            </StackPanel>
                    </Border>
                </Popup>
            </Grid>

每当我尝试在Popup 元素中设置Width="Center"Height="Center" 时,如下所示

<Popup x:Name="trialPopup" HorizontalAlignment="Center" VerticalAlignment="Center">

左上角是居中的,因此弹出窗口不在控件本身的中间。有没有办法根据弹出窗口的宽度和高度以及页面的宽度和高度来计算正确的尺寸并相应地应用它?或者有没有办法在 XAML 中正确执行此操作?另外,不透明度值没有设置?

【问题讨论】:

    标签: xaml windows-phone-7 windows-phone-8 popup


    【解决方案1】:

    页面中心:

    <Popup PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}" Placement="Center" />
    

    窗口中心:

    <Popup PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Placement="Center" />
    

    【讨论】:

      【解决方案2】:

      尝试使用 Popup 的 Offset 属性:

      trialPopup.VerticalOffset = - ((FrameworkElement)trialPopup.Child).ActualHeight / 2;
      trialPopup.HorizontalOffset = -((FrameworkElement)trialPopup.Child).ActualWidth / 2;
      

      为 Popup.Child 设置不透明度时会起作用,但我不知道为什么它对 Popup 不起作用。

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 2012-06-08
        相关资源
        最近更新 更多