如何在Windows Phone中实现弹出窗口

时间:2014-11-10 07:14:57

标签: wpf xaml popup windows-phone-8.1

我实现了一个模板化控件,该控件可以作为虚拟键盘按钮使用 - 当你按住它时,它会显示一个弹出窗口,其中包含可供选择的其他选项。

我通过以下方式更少地实现了弹出窗口:

<Grid>
    <Border>Content</Border>
    <Grid x:Name="gPopup" Visibility="Collapsed">
         <StackPanel x:Name="spSubItems" Orientation="Horizontal" />
    </Grid>
</Grid>

我通过将可见性更改为可见并设置顶部和底部的负边距来显示弹出窗口。但是,当我这样做,并且当弹出窗口实际上大于控件时,控件正在调整大小以匹配其大小 - 尽管事实上,它不在内部: enter image description here enter image description here

如何实现弹出窗口,以便它不会扩展它所使用的容器?并且容器仍然会匹配其内容的大小?


编辑:回复评论和回答

我不确定我是否理解正确。这是一张带有解释的图片:

enter image description here

我希望在显示弹出窗口后保持原始容器的大小不变。我不确定WrapPanel或DockPanel如何帮助我。

3 个答案:

答案 0 :(得分:1)

解决方案只是使用Popup而不是定位Grid。

答案 1 :(得分:0)

示例 - 创建网格

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
   <!-- Setting a Rectangle having transparent background which set the
   Visibility of popup -->
   <Rectangle Name="popupRect" Fill="#80000000" Visibility="Collapsed"/>
   <!—Here in the above Code we are just filling the rectangle With the transparent BackGround -->
   <!—Creating A Border -->
   <Border Name="popupBorder" Background="{StaticResource PhoneChromeBrush}" BorderBrush="Red" BorderThickness="2"
   HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
   <!-- Creating A grid Inside the Border and Rectangle -->
</Grid>

创建应显示弹出窗口的事件(取消和显示) -

private void cancelButton_Click(object sender, RoutedEventArgs e)
{
   popupRect.Visibility = Visibility.Collapsed;
   popupBorder.Visibility = Visibility.Collapsed;
}
private void popupButton_Click(object sender, RoutedEventArgs e)
{
   popupRect.Visibility = Visibility.Visible;
   popupBorder.Visibility = Visibility.Visible;
}

我猜它会起作用。

答案 2 :(得分:0)

像spook说的那样,将你的gPopup Grid放在一个Popup元素中并通过打开弹出窗口来显示它。这不会影响主视觉树。

嵌入式网格嵌入边界的原因是外部网格必须扩展以保持pGrid并且边界扩展以填充外部网格。