如何在Silverlight中弹出PopUp控件

时间:2014-07-18 09:46:54

标签: c# .net silverlight popup silverlight-5.0

我正在尝试弹出一个包含一些按钮的PopUp控件,但不幸的是,当我运行我的应用程序时,它会在左上角弹出。我的意思是(0,0)[解释(x,y)轴]。

我这样做的代码是:

<Popup x:Name="myPopup" Margin="-34,0,-31,0" Grid.Row="2" Grid.Column="1" Height="78" VerticalAlignment="center" HorizontalAlignment="center">
    <Border Background="Silver" BorderThickness="2" BorderBrush="Black" Height="224" Width="371">
        // Here is a grid which contains all my buttons and otherthing on popup
    </Border>
</Popup>

有人可以帮助我在中心弹出它吗?(即使我完成HorizontalAlignment="center"VerticalAlignment="center"也不行。

3 个答案:

答案 0 :(得分:1)

您可以使用ChildWindow,它会自动显示在屏幕中央

或者如果您想手动将弹出窗口置于中心,可以手动调整HorizontalOffsetVerticalOffset

double positioningX = (Application.Current.Host.Content.ActualWidth / 2) - (widthOfDialog / 2);
double positioningY = (Application.Current.Host.Content.ActualHeight / 2) - (heightOfDialog / 2);
if (positioningX > 0)
    popup.HorizontalOffset = positioningX;
if (positioningY > 0)
    popup.VerticalOffset = positioningY;

答案 1 :(得分:0)

使用VerticalOffset和Horizo​​ntalOffset

答案 2 :(得分:0)

在我的案例中这样解决了:

 double width = Convert.ToDouble(HtmlPage.Window.Eval("screen.width"));
                double height = Convert.ToDouble(HtmlPage.Window.Eval("screen.height"));

                AdvertisingControl ac = new AdvertisingControl(); // Your user control here..

                this.p = new Popup()
                {
                    VerticalOffset = (height - ac.Height) / 2.0,
                    HorizontalOffset = (width - ac.Width) / 2.0,
                    Width = ac.Width,
                    Height = ac.Height,
                    Child = ac,
                };

                this.p.IsOpen = true;