在窗口中居中WPF弹出窗口

时间:2016-02-01 17:54:10

标签: wpf xaml popup converter

我有一个WPF应用程序,我试图将弹出窗口置于主窗口中心并让它填满窗口,但没有溢出边框。但是,由于某种原因,弹出窗口显示为偏移:Popup offset。从图中可以看出,弹出窗口是低而向右。下面是弹出窗口和大小转换器的代码:

XAML:

            <Popup Name="MenuPopup" Closed="MenuPopup_Closing" Placement="Center" PlacementTarget="{Binding ElementName=Window1}" IsOpen="False" 
           AllowsTransparency="True" StaysOpen="False" Grid.RowSpan="2" PopupAnimation="Fade">
            <Grid Name="MenuGrid" MouseDown="Popups_MouseDown"  Height="{Binding ActualHeight, ElementName=Window1, Converter={StaticResource windowHeightConverter}}" Width="{Binding ActualWidth,ElementName=Window1,Converter={StaticResource windowWidthConverter}}">
                <controls:MenuView />
                <Grid.Background>
                    <SolidColorBrush Color="{DynamicResource GreyBackgroundColor}" Opacity="{DynamicResource BackgroundOpacity}"/>
                </Grid.Background>
            </Grid>
        </Popup>

转换器:

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double _height = SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight;
        return ((double)value - _height);            
    }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double _width = SystemParameters.ResizeFrameVerticalBorderWidth*2;
        return ((double)value - _width);            
    }

我可以通过使用水平和垂直偏移强制弹出窗口来解决问题,但这似乎是一个脏黑客,可能不适用于不同的Windows主题或操作系统。

我是否遗漏了一些简单的内容,或者有人有任何想法吗?

1 个答案:

答案 0 :(得分:0)

所以我能够让弹出窗口完全按照你想要的代码工作:

c = 0
for each claimType in claimTypes
    for each state in StateJuris
        c = c + wsf.CountIfs(Sheets(claimstab).Range("B2:B" & Tab1LastRow), HomeOffice, Sheets(claimstab).Range("C2:C" & Tab1LastRow), DataSet, Sheets(claimstab).Range("E2:E" & Tab1LastRow), ClaimType.value, Sheets(claimstab).Range("G2:G" & Tab1LastRow), State.value)
    next state
next claimType

lineitem.Offset(0, 16) = c

XAML:

            Popup MenuPopup = (Popup)this.Resources["popup"];
        double dTitleHeight = SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight;
        double dVerticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
        MenuPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Absolute;
        MenuPopup.HorizontalOffset = this.PointToScreen(new Point(0, 0)).X - dVerticalBorderWidth *2 ;
        MenuPopup.VerticalOffset = this.PointToScreen(new Point(0, 0)).Y - dTitleHeight - dVerticalBorderWidth;
        MenuPopup.Height = this.ActualHeight;
        MenuPopup.Width = this.ActualWidth ;
        MenuPopup.IsOpen = true;