WPF使用popup控件实现弹出式提示
利用Popup控件实现弹出式提示窗口信息
在wpf桌面程序开发中有时候我们需要做到类似web端那种附着标签悬浮信息提示窗效果,其实利用Popup控件就可以实现类似的悬浮标签功能 然后在后台通过对Popup控件进行展示状态控制隐藏或展示,此外利用这个控件还可以实现下拉菜单 以及气泡提示窗等等的效果
下面是一个实现基本悬浮下拉菜单的简单例子
//前端代码
<Grid>
<Button x:Name="PopButton" Click="PopButton_Click" Content="弹出式提示窗口信息"
VerticalAlignment="Top" />
<Popup x:Name="Pop" PopupAnimation="Slide" Width="100" Height="100"
PlacementTarget="" Placement="Bottom" AllowsTransparency="True" StaysOpen="False">
<!--这里也可以自己绘制成其他的展示效果 比如弹出气泡窗之类的样式 下面这个是比较简单的下拉菜单效果-->
<Border Background="#FFCFCFCF" CornerRadius="5" BorderBrush="#FF000000" BorderThickness="1">
<ItemsControl>
<Label Content="我是第1行" />
<Label Content="我是第2行" />
<Label Content="我是第3行" />
<Label Content="我是第4行"/>
</ItemsControl>
</Border>
</Popup>
</Grid>
属性说明:
- PlacementTarget:绑定控件的目标控件(附着目标显示) 这里是绑定的按钮
- Placement: 设置气泡控件的展示方向 这里设置的是按钮底部显示
- PopupAnimation:控件显示是的动画效果 这里Slide 滑动效果
后端代码:
private void PopButton_Click(object sender, RoutedEventArgs e) {
Pop.IsOpen = true;//设置为打开状态
}