『QQ:1353814576』

WPF使用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>

属性说明:

  1. PlacementTarget:绑定控件的目标控件(附着目标显示) 这里是绑定的按钮
  2. Placement: 设置气泡控件的展示方向 这里设置的是按钮底部显示
  3. PopupAnimation:控件显示是的动画效果 这里Slide 滑动效果

后端代码:

private void PopButton_Click(object sender, RoutedEventArgs e) {
                   Pop.IsOpen = true;//设置为打开状态
}

WPF使用popup控件实现弹出式提示