Popup Not Showing

无人久伴 提交于 2019-12-11 09:39:17

问题


On a tap event I would like to show a popup all within code behind, but my popup is not displaying?

void PopupDisplay_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        if (sender != null)
        {
            p = new Popup
            {
                Width = 480,
                Height = 580,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                VerticalAlignment = System.Windows.VerticalAlignment.Center                    
            };

            Border b = new Border();
            b.BorderBrush = new SolidColorBrush(Colors.Gray);
            b.BorderThickness = new Thickness(2);
            b.Margin = new Thickness(10, 10, 10, 10);

            p.Child = b;
            p.IsOpen = true;
        }
    }

回答1:


Think you're trying to Popup over a top-level control like a Pivot which is very buggy.

See Popup with Pivots

If it was a Grid, it would pop up without problem. To fix this you will have to add it to the same visual level as the Pivot like so:

<Grid x:Name="ContentPanel" Margin="0,0,0,0">
    <phone:Pivot x:Name="MainDisplay">
    <!-- more code -->
    </phone:Pivot>       
</Grid>

Then in your code-behind

// I made with a thickness of 100, so we can see the border better
Popup p;

p = new Popup
{
    Width = 480,
    Height = 580,
    VerticalOffset = 0
};

Border b = new Border();
b.BorderBrush = new SolidColorBrush(Colors.Red);
b.BorderThickness = new Thickness(100);
b.Margin = new Thickness(10, 10, 10, 10);
b.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
b.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

p.Child = b;

// add it to the same level as the pivot to over ride pivot
this.ContentPanel.Children.Add(p);

p.IsOpen = true;


来源:https://stackoverflow.com/questions/26701190/popup-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!