在Metro应用程序中拉伸并居中弹出

本文关键字:Metro 应用程序 | 更新日期: 2023-09-27 17:59:38

我有一个弹出窗口,但在视觉设计器中,它既不拉伸也不居中。为什么会这样?我怎样才能达到这种效果?

这就是我在视觉树中设置它的方式:

    <Popup x:Name="namePromptPopup" IsLightDismissEnabled="True" Grid.Row="1" Grid.Column="1">
        <Popup.ChildTransitions>
            <TransitionCollection>
                <PopupThemeTransition/>
            </TransitionCollection>
        </Popup.ChildTransitions>
        <StackPanel>
            <Border BorderBrush="White" BorderThickness="5" >
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF600F0F"/>
                        <GradientStop Color="#FFB81C1C" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBox Text="Generic Name" FontSize="38" HorizontalAlignment="Center" Width="320" />
                    <Button Content="Submit" Click="namePromptSubmit" HorizontalAlignment="Center" />
                </StackPanel>
            </Border>
        </StackPanel>
    </Popup>

看起来弹出控件根本不响应布局。

在Metro应用程序中拉伸并居中弹出

我已经查看了网络,多亏了这个家伙,他有了一个可以使用的解决方案。

他只是计算窗口的宽度/高度,并使用弹出窗口的宽度和高度来给出偏移。

//Here is where I get a bit tricky.  You see namePromptPopup.ActualWidth will show
//the full screen width, and not the actual width of the popup, and namePromptPopup.Width
//will show 0 at this point.  So we needed to calculate the actual
//display width.  I do this by using a calculation that determines the
//scaling percentage from the height, and then calculates that against my
//original width coding.
namePromptPopup.HorizontalOffset = (currentWindow.Bounds.Width / 2) - (400 / 2);
namePromptPopup.VerticalOffset = (currentWindow.Bounds.Height / 2) - (150 / 2);

再次引用