一、介绍
常用的属性
IsOpen: 布尔值,指示 Popup 控件是否显示。
比如说:IsOpen="{Binding ElementName=PCheckBox,Path=IsChecked}" 是通过绑定CheckBox的IsChecked属性来控制Popup的弹出。
StaysOpen: 布尔值,指示在 Popup 控件失去焦点的时候,是否关闭 Popup 控件的显示。
PopupAnimation:指示显示窗口时是否使用动画,只有在 AllowsTransparency 等于true时此属性才有用
Popup 窗口本身是一个不可见的元素,只有在窗口上放置了信息后才能显示
Popup的定位方式与一般控件的定位方法不一样, Popup 使用五个属性来设定位置信息:
PlacementTarget:表示Popup控件的放置的位置依赖的对象,这个通常使用绑定的方式来标明Popup控件停靠的目标,如果没有为属性为 NULL,则 Popup 定位相对于屏幕的左上角。
Placement:一个枚举值,指定 Popup 控件的定位方式。通过设置 PlacementTarget、PlacementRectangle、Placement、HorizontalOffset 和 VerticalOffsetProperty 属性来定位弹出项
PlacementRectangle:设定一个矩形,在 Popup 控件显示时,位置将相对于此矩形来显示,此矩形的位置也相对于PlacementTarget 属性所设定的控件
HorizontalOffset:指定一个值,指示 Popup 的位置所需水平移动多少个象素
VerticalOffset:指定一个值,指示 Popup 的位置所需垂直移动多少个象素
二、实例
点击button,popup显示
<StackPanel Oriention="Horizontal">
<Button x:Name="gridBtn" ToolTip="网格"
Click="GridBtn_Click">
<Image Source="网格.png"></Image>
</Button>
<Popup x:Name="popGrid" Width="220" Height="180"
StaysOpen="False"
IsOpen="False"
AllowsTransparency="True"
PopupAnimation="Slide"
PlacementTarget="{Binding ElementName= gridBtn}"
Placement="Bottom">
<Canvas Background="{StaticResource MyBackground}">
<Grid>
<TextBlock Text="间距" Width="100" Foreground="White" VerticalAlignment="Center" Margin="32,33,68,151"/>
<TextBlock Text="大小" Width="100" Foreground="White" VerticalAlignment="Center" Margin="32,70,68,115"/>
<TextBlock Text="颜色" Width="100" Foreground="White" VerticalAlignment="Center" Margin="32,107,68,78"/>
<ComboBox Width="100" VerticalAlignment="Center" Foreground="White" SelectedIndex="0"
Margin="76,30,24,149">
</Grid>
</Canvas>
</Popup>
</StackPanel>
private void GridBtn_Click(object sender, RoutedEventArgs e)
{
popGrid.IsOpen = true;
}
注意:popup必须用canvas填充,直接用grid填充会达不到效果,大家可以测试下
参考:
https://blog.csdn.net/coolfeiweb/article/details/25218245
https://www.cnblogs.com/seekdream/p/5579221.html
https://blog.csdn.net/xiaodenanhai/article/details/9129247?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase