จะแน่ใจได้อย่างไรว่าตัวควบคุมป๊อปอัปตรงกับเพจหลักเมื่อมีการปรับขนาดพาเรนต์ UWP

ฉันมีป๊อปอัปที่จะเติมเต็มทั้งหน้าเมื่อเปิด

<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>

ป๊อปอัปคือ UserControl

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

ป้อนคำอธิบายรูปภาพที่นี่

  1. เมื่อป๊อปอัปปรากฏขึ้น

ป้อนคำอธิบายรูปภาพที่นี่

  1. ปิดป๊อปอัป ปรับขนาดหน้า จากนั้นเปิดป๊อปอัปอีกครั้ง โปรดสังเกตว่ามันไม่ตรงกับขนาดใหม่ของหน้าคอนเทนเนอร์ แม้ว่า Width และ Height จะผูกไว้กับ gridRoot ก็ตาม ฉันต้องตั้งค่า Width และ Height ใหม่สำหรับป๊อปอัปด้วยตนเองหรือไม่ เหตุใดฉันจึงไม่บรรลุเป้าหมายนี้ด้วยการผูกมัด ปัญหานี้ยังปรากฏบนมือถือในช่วง 'OrientationChanged'

ป้อนคำอธิบายรูปภาพที่นี่


person PutraKg    schedule 19.03.2017    source แหล่งที่มา
comment
ใช้ตำแหน่งเมาส์ได้ไหม?   -  person lindexi    schedule 19.03.2017
comment
คุณช่วยอธิบายรายละเอียดได้ไหม?   -  person PutraKg    schedule 19.03.2017
comment
การตั้งค่า ActualWidth เป็นแหล่งที่มาของการเชื่อมโยงมักจะไม่ทำงานตามที่คาดไว้ ดูสิ่งนี้สำหรับข้อมูลเพิ่มเติม   -  person Decade Moon    schedule 19.03.2017
comment
ในบริบทของแอปของคุณ คุณสามารถหลีกเลี่ยงการใช้ Flyout และตั้งค่าคุณสมบัติ PlacementMode=Full ได้หรือไม่   -  person Lindsay    schedule 21.03.2017
comment
@Lindsay น่าเสียดายที่ไม่เพราะฉันมีป๊อปอัปหลายอันซึ่งแต่ละอันมีการควบคุมและฟังก์ชั่นที่แตกต่างกัน   -  person PutraKg    schedule 21.03.2017


คำตอบ (2)


ตามความคิดเห็นของ 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 สำหรับการเชื่อมโยงแหล่งที่มาเป็น พวกเขาไม่ได้เพิ่มเหตุการณ์ PropertyChanged

แม้ว่าจะมีฟิลด์สำรอง ActualWidthProperty แต่ ActualWidth จะไม่เพิ่มการแจ้งเตือนการเปลี่ยนแปลงคุณสมบัติ และควรถือเป็นคุณสมบัติ CLR ปกติ ไม่ใช่คุณสมบัติการขึ้นต่อกัน

เพื่อวัตถุประสงค์ของการเชื่อมโยง ElementName นั้น ActualWidth จะไม่โพสต์การอัปเดตเมื่อมีการเปลี่ยนแปลง (เนื่องจากลักษณะการคำนวณแบบอะซิงโครนัสและรันไทม์) อย่าพยายามใช้ ActualWidth เป็นแหล่งการโยงสำหรับการโยง ElementName หากคุณมีสถานการณ์ที่จำเป็นต้องมีการอัปเดตตาม ActualWidth ให้ใช้ตัวจัดการ SizeChanged

person PutraKg    schedule 20.03.2017

@PutraKg มีวิธีที่ยอดเยี่ยม

แต่ฉันมีสองวิธีในการแก้ปัญหา

อันแรกคือตั้งค่า VerticalAlignment="Center" HorizontalAlignment="Center" ที่สามารถทำให้ป๊อปอัปอยู่ตรงกลาง

แต่ฉันคิดว่าคุณไม่พอใจที่จะเอามันไว้ตรงกลาง

วิธีที่ดีคือใช้ตำแหน่งหน้าจอ

คุณสามารถรับตำแหน่งบนหน้าจอของ Grid และทำให้เป็นป๊อปอัปได้

ในปุ่มเปิด

    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;
    }
person lindexi    schedule 20.03.2017
comment
ใช่ ฉันไม่ต้องการวางป๊อปอัปไว้ตรงกลาง น่าเสียดายสำหรับวิธีแก้ปัญหานี้ ป๊อปอัปจะตรงกับคอนเทนเนอร์หลักเมื่อคลิกเท่านั้น เมื่อมีการปรับขนาดพาเรนต์ในขณะที่เปิดป๊อปอัป มันจะไม่ปรับขนาดโดยอัตโนมัติ ขอบคุณสำหรับข้อเสนอแนะต่อไป - person PutraKg; 20.03.2017