如何在窗口商店应用程序中居中弹出窗口

时间:2014-10-28 14:49:03

标签: c# xaml windows-store-apps

我有一个带有自定义弹出窗口的用户控件(UserHelperButton)。我已手动设置垂直和水平偏移以在正确的位置显示弹出窗口。现在我需要将它的位置更新为中心。我无法将其置于x轴和垂直方向上。

在根窗口上,我添加了在不同位置添加UserHelperButton

<Grid Margin="0,0,0,25">
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="120" />
    <ColumnDefinition Width="50" />
    <ColumnDefinition Width="50" />
    <ColumnDefinition Width="120" />
    <ColumnDefinition Width="50" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="120" />
    <ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<UserControls:UserLabel Text="Varnost v dvoje" Grid.Column="0" />
<UserControls:UserCheckBox x:Name="cbxDouble" Grid.Column="1" cbxClick="cbxDouble_cbxClick" />
<UserControls:UserHelperButton Grid.Column="2" HelperText="V primeru, da se zavarujeta dve osebi, obe zavarovani osebi pridobita nižjo premijo za sklenjena zavarovanja." />
<UserControls:UserLabel x:Name="lblRefNum" Text="Referenčna številka" Grid.Column="3" />
<UserControls:UserCheckBox x:Name="cbxRefNum" Grid.Column="4" cbxClick="cbxRefNum_cbxClick" />
<UserControls:UserTextBox x:Name="txbRefNum" Grid.Column="5" ValidationMessage="Vnesite referenčno številko!" />
<UserControls:UserLabel Text="Zaposlen na Pošta Slovenija" Grid.Column="6" />
<UserControls:UserCheckBox x:Name="cbxPS" Grid.Column="7" cbxClick="cbxPS_cbxClick" />

<Grid Margin="0,25,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="500" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                        <UserControls:UserLabel Grid.Column="0" x:Name="lblInsurance" Text="Priporočena varnost do 65. leta" LabelMode="Heading" ValidationMessage="Vsota zavarovanj je premajhna!"  />
                        <UserControls:UserHelperButton x:Name="helper2" Grid.Column="2" />
                    </Grid>

userControl xaml:

<Grid x:Name="grid" Margin="0" MaxHeight="50" Canvas.ZIndex="4" VerticalAlignment="Center">
<Button x:Name="btnHelper" AutomationProperties.Name="" Style="{StaticResource HelpAppBarButtonStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" Height="70" Click="btnHelper_Click" />
    <Popup x:Name="popHelper" IsLightDismissEnabled="True" VerticalOffset="-150" HorizontalOffset="-50">
        <Border BorderThickness="25" CornerRadius="25" BorderBrush="Gray" Background="Gray">
            <Grid Background="Gray" Width="400" Height="300">
                <Grid.RowDefinitions>
                    <RowDefinition Height="60" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <local:UserLabel Text="Pomoč" LabelMode="Heading" VerticalAlignment="Top" />
                <Button x:Name="btnClose" AutomationProperties.Name="" Grid.Row="0" Style="{StaticResource NoAppBarButtonStyle}" HorizontalAlignment="Right" VerticalAlignment="Top" Padding="12,0,0,0" Height="100" Click="btnClose_Click" RenderTransformOrigin="0.5,0.5" >
                    <Button.RenderTransform>
                        <CompositeTransform TranslateY="-15"/>
                    </Button.RenderTransform>
                </Button>
                    <WebView x:Name="webviewControl" Grid.Row="1" Margin="0,20,0,0" Height="200" Width="400" />
            </Grid>
        </Border>
    </Popup>

在加载事件中,我使用帮助文本填充弹出窗口

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        string html = "<html><body style=\"font-family: 'Segoe UI'; background-color: gray; color: white; margin: 0;\">" + g_text + "</body></html>";

        var fragment = HtmlFormatHelper.GetStaticFragment(HtmlFormatHelper.CreateHtmlFormat(html));

        webviewControl.NavigateToString(fragment);
    }

那我怎么能这样做呢?

THX

百吉

1 个答案:

答案 0 :(得分:0)

我这样做:

    private void popup_Loaded(object sender, RoutedEventArgs e)
    {
        popup.HorizontalOffset = (Window.Current.Bounds.Width - popupGrid.Width) / 2;
        popup.VerticalOffset = (Window.Current.Bounds.Height - popupGrid.Height) / 2;
    }