Xaml 调整父控件的大小时,如何确保弹出控件与其父页面匹配?超宽带

Xaml 调整父控件的大小时,如何确保弹出控件与其父页面匹配?超宽带,xaml,popup,uwp,win-universal-app,Xaml,Popup,Uwp,Win Universal App,我有一个弹出窗口,打开时会填满整个页面 <Grid x:Name="gridRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Button Content="Open" HorizontalAlignment="Center" Click="{x:Bind viewModel.OpenPopup}" /> <Popup x:Name="popupCorre

我有一个弹出窗口,打开时会填满整个页面

<Grid x:Name="gridRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Open" HorizontalAlignment="Center" Click="{x:Bind viewModel.OpenPopup}" />
    <Popup x:Name="popupCorrect" VerticalAlignment="Top" IsOpen="{Binding IsOpen}" IsLightDismissEnabled="False">
        <Popup.ChildTransitions>
            <TransitionCollection>
                <PaneThemeTransition Edge="Left" />
            </TransitionCollection>
        </Popup.ChildTransitions>
        <uc:MyPopup  Width="{Binding ElementName=gridRoot, Path=ActualWidth}" Height="{Binding ElementName=gridRoot, Path=ActualHeight}"/>
    </Popup>
</Grid>

弹出窗口是一个用户控件

<Grid Background="Red">
    <Button Content="Close" HorizontalAlignment="Center" Click="{x:Bind viewModel.ClosePopup}" />
</Grid>

  • 页面
  • 当弹出窗口显示时
  • 关闭弹出窗口,调整页面大小,然后重新打开弹出窗口。请注意,它与容器页面的新大小不匹配,即使其
    宽度
    高度
    绑定到
    gridRoot
    。是否必须手动为弹出窗口设置新的
    宽度
    高度
    ?为什么我不能通过绑定实现这一点?此问题也会在“方向更改”期间出现在手机上

  • 根据Decade Moon的评论,这是如何调整弹出窗口的大小,以便在其大小更改时与父容器相匹配

    在代码隐藏中创建依赖项属性

        public double PageWidth
        {
            get { return (double)GetValue(PageWidthProperty); }
            set { SetValue(PageWidthProperty, value); }
        }
    
        public static readonly DependencyProperty PageWidthProperty =
            DependencyProperty.Register("PageWidth", typeof(double), typeof(GamePage), new PropertyMetadata(0d));
    
    
    
        public double PageHeight
        {
            get { return (double)GetValue(PageHeightProperty); }
            set { SetValue(PageHeightProperty, value); }
        }
    
        public static readonly DependencyProperty PageHeightProperty =
            DependencyProperty.Register("PageHeight", typeof(double), typeof(GamePage), new PropertyMetadata(0d));
    
    更新
    SizeChanged
    事件上的值

        private void GamePage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width > 0d && e.NewSize.Height > 0d)
            {
                PageWidth = e.NewSize.Width;
                PageHeight = e.NewSize.Height;
            }
        }
    
    然后在XAML中,只需使用x:Bind绑定弹出窗口的宽度和高度

            <Popup x:Name="popupCorrect" VerticalAlignment="Top" IsOpen="{Binding IsPopupCorrectOpen, Mode=TwoWay}" IsLightDismissEnabled="False">
            <Popup.ChildTransitions>
                <TransitionCollection>
                    <PaneThemeTransition Edge="Left" />
                </TransitionCollection>
            </Popup.ChildTransitions>
            <uc:PopupCorrect Width="{x:Bind PageWidth, Mode=TwoWay}" Height="{x:Bind PageHeight, Mode=TwoWay}"/>
        </Popup>
    
    
    
    非常直截了当。请记住不要使用
    ActualWidth
    ActualHeight
    属性将源代码绑定为

    尽管它有一个ActualWidthProperty支持字段,但ActualWidth不会引发属性更改通知,应该将其视为常规CLR属性而不是依赖性属性

    出于ElementName绑定的目的,ActualWidth在更改时不会发布更新(由于其异步和运行时计算的性质)。不要尝试将ActualWidth用作ElementName绑定的绑定源。如果您有需要基于实际宽度进行更新的场景,请使用SizeChanged处理程序


    @普特拉克有一个很棒的方法

    但我有两种方法来解决它

    第一个是设置垂直对齐=“中心”水平对齐=“中心”,可以在中心弹出窗口

    但我认为你不满足于把它放在中心

    最好的方法是使用屏幕位置

    您可以获得网格的屏幕位置并使其弹出

    打开按钮

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            var grid = (UIElement)popupCorrect.Parent; //get grid
            var p = grid.TransformToVisual (Window.Current.Content).TransformPoint(new Point(0, 0)); //get point
            popupCorrect.HorizontalOffset = p.X;
            popupCorrect.VerticalOffset = p.Y;
            popupCorrect.IsOpen = !popupCorrect.IsOpen;
        }
    

    可以使用鼠标位置吗?可以详细说明吗?将
    ActualWidth
    设置为绑定源通常不会按预期工作。有关更多信息,请参阅。在您的应用程序上下文中,您是否可以使用
    弹出按钮
    并设置
    PlacementMode=Full
    属性?Lindsay遗憾的是,否,因为我有几个弹出窗口,每个弹出窗口包含不同的控件和功能。是的,我不想将弹出窗口放在中间。不幸的是,对于此解决方案,单击时弹出窗口仅与父容器匹配。当弹出窗口打开时调整父对象的大小时,它不会自动调整大小。无论如何,谢谢你的建议。