如何让Popup出现在特定的角落

我希望我的Popup始终出现在特定的角落(例如右下角),无论我的View.


我尝试使用HorizontalAlignment但VerticalAlignment它并没有真正起作用。


这是我的代码:


<Grid x:Name="Output" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="1">

    <Popup x:Name="StandardPopup">

        <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" 

            Background="{StaticResource ApplicationPageBackgroundThemeBrush}"

            BorderThickness="2" Width="500" Height="500">

            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

                <WebView x:Name="WebView1" Source="https://www.bing.com/" Width="490" Height="490" HorizontalAlignment="Center"/>

            </StackPanel>

        </Border>

    </Popup>

</Grid>


明月笑刀无情
浏览 127回答 2
2回答

qq_遁去的一_1

您可以创建一个 UserControl 来帮助您实现这一目标。创建一个名为 TestPopup 的新 UserControl<UserControl&nbsp; &nbsp; ...&nbsp; &nbsp; >&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <Grid HorizontalAlignment="Right" VerticalAlignment="Bottom">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Background="{StaticResource ApplicationPageBackgroundThemeBrush}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BorderThickness="2" Width="500" Height="500">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <WebView x:Name="WebView1" Source="https://www.bing.com/" Width="490" Height="490" HorizontalAlignment="Center"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Border>&nbsp; &nbsp; &nbsp; &nbsp; </Grid>&nbsp; &nbsp; </Grid></UserControl>后台代码:public sealed partial class TestPopup : UserControl{&nbsp; &nbsp; private Popup _popup = null;&nbsp; &nbsp; public TestPopup()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; // If you need to top placement, please comment out the Width/Height statement below&nbsp; &nbsp; &nbsp; &nbsp; this.Width = Window.Current.Bounds.Width;&nbsp; &nbsp; &nbsp; &nbsp; this.Height = Window.Current.Bounds.Height;&nbsp; &nbsp; &nbsp; &nbsp; //Assign the current control to the Child property of the popup. The Child property is what the popup needs to display.&nbsp; &nbsp; &nbsp; &nbsp; _popup = new Popup();&nbsp; &nbsp; &nbsp; &nbsp; _popup.Child = this;&nbsp; &nbsp; }&nbsp; &nbsp; public void ShowPopup()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _popup.IsOpen = true;&nbsp; &nbsp; }&nbsp; &nbsp; public void HidePopup()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _popup.IsOpen = false;&nbsp; &nbsp; }}在需要时使用 C# 代码引用public sealed partial class MainPage : Page{&nbsp; &nbsp; private TestPopup _popup = new TestPopup();&nbsp; &nbsp; public MainPage()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; _popup.ShowPopup();&nbsp; &nbsp; }}保证任意宽度下都在右下角,可以监听页面的SizeChanged事件。private void Page_SizeChanged(object sender, SizeChangedEventArgs e){&nbsp; &nbsp; _popup.Width = Window.Current.Bounds.Width;&nbsp; &nbsp; _popup.Height = Window.Current.Bounds.Height;&nbsp; &nbsp; // If you need to use the Width/Height of the page&nbsp; &nbsp; // _popup.Width = e.NewSize.Width;&nbsp; &nbsp; // _popup.Height = e.NewSize.Height;}此致。

慕少森

遗憾的是弹出窗口不支持对齐方式,您必须使用偏移值注意:不要忘记您可以使用偏移绑定,从而通过一些转换器魔法获得您想要的行为。<Popup HorizontalOffset="20"         VerticalOffset="10">     <!--Content-->     </Popup>
打开App,查看更多内容
随时随地看视频慕课网APP