WPF气泡样式弹窗效果代码分享

 更新时间:2016年09月02日 14:16:41   作者:Amiry  
这篇文章主要为大家详细介绍了WPF气泡样式弹窗效果的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

页面设计需求,做了一个气泡形状的弹出框,效果如下:

 

设计思路如下:

1. 使用Path绘制气泡的尖尖,将这个放到顶层;

2. 在用border绘制长方形框,将这个放到底层,并且设置Margin值,使得Path图层和border看起来衔接在一起。 

代码如下:

<Window x:Class="BubblePanelTest.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
 <Window.Resources>
  <Style TargetType="Label" x:Key="TopBubblePanel">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Label}">
      <Grid>
       <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Top" Background="Yellow" HorizontalAlignment="Left" Margin="0,8.5,0,0" Padding="5">
        <ContentPresenter />
       </Border>
       <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" Background="Transparent">
        <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
         <Path.Data>
          <PathGeometry Figures="M 0,10
         L 0,10,5,0
         L 5,0,10,10
         "/>
         </Path.Data>
        </Path>
       </Canvas>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
  <Style TargetType="Label" x:Key="BottomBubblePanel">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Label}">
      <Grid>
       <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Bottom" Margin="0,0,0,8.5" Background="Yellow" HorizontalAlignment="Left" Padding="5">
        <ContentPresenter />
       </Border>
       <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10,0,0,0" Background="Transparent">
        <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
         <Path.Data>
          <PathGeometry Figures="M 0,0
         L 0,0,5,10
         L 5,10,10,0
         "/>
         </Path.Data>
        </Path>
       </Canvas>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
  <Style TargetType="Label" x:Key="LeftBubblePanel">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Label}">
      <Grid>
       <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Top" Margin="8.5,0,0,0" Background="Yellow" HorizontalAlignment="Left" Padding="5">
        <ContentPresenter />
       </Border>
       <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" Background="Transparent">
        <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
         <Path.Data>
          <PathGeometry Figures="M 10,0
         L 10,0,0,5
         L 0,5,10,10
         "/>
         </Path.Data>
        </Path>
       </Canvas>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
  <Style TargetType="Label" x:Key="RightBubblePanel">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Label}">
      <Grid HorizontalAlignment="Left">
       <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,8.5,0" Background="Yellow" Padding="5">
        <ContentPresenter />
       </Border>
       <Canvas Width="10" Height="10" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,0,0" Background="Transparent">
        <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
         <Path.Data>
          <PathGeometry Figures="M 0,0
         L 0,0,10,5
         L 10,5,0,10
         "/>
         </Path.Data>
        </Path>
       </Canvas>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
 </Window.Resources>
 <StackPanel>
  <Label Style="{StaticResource TopBubblePanel}" Tag="Top" Margin="2">
   <StackPanel>
    <StackPanel Orientation="Horizontal">
     <TextBlock Text="abc" />
     <TextBox Width="80"/>
    </StackPanel>
   </StackPanel>
  </Label>
  <Label Style="{StaticResource BottomBubblePanel}" Tag="Top" Margin="2">
   <StackPanel>
    <StackPanel Orientation="Horizontal">
     <TextBlock Text="abc" />
     <TextBox Width="80"/>
    </StackPanel>
   </StackPanel>
  </Label>
  <Label Style="{StaticResource LeftBubblePanel}" Tag="Top" Margin="2">
   <StackPanel>
    <StackPanel Orientation="Horizontal">
     <TextBlock Text="abc" />
     <TextBox Width="80"/>
    </StackPanel>
   </StackPanel>
  </Label>
  <Label Style="{StaticResource RightBubblePanel}" Tag="Top" Margin="2">
   <StackPanel>
    <StackPanel Orientation="Horizontal">
     <TextBlock Text="abc" />
     <TextBox Width="80"/>
    </StackPanel>
   </StackPanel>
  </Label>
  <StackPanel Orientation="Horizontal" Margin="0,30,0,0">
   <Button Name="btnTestPopup1" Width="100" Height="30" Content="Bottom" Click="btnTestPopup1_Click" />
   <Button Name="btnTestPopup2" Width="100" Height="30" Content="Top" Click="btnTestPopup2_Click" />
   <Button Name="btnTestPopup3" Width="100" Height="30" Content="Right" Click="btnTestPopup3_Click" />
   <Button Name="btnTestPopup4" Width="100" Height="30" Content="Left" Click="btnTestPopup4_Click" />
  </StackPanel>
  <Popup Name="pop1" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=btnTestPopup1}" Placement="Bottom" >
   <Label Style="{StaticResource TopBubblePanel}" Tag="Top">
    <StackPanel>
     <StackPanel Orientation="Horizontal">
      <TextBlock Text="abc" />
      <TextBox Width="80" Name="txtTest1" />
     </StackPanel>
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
      <Button Content="确定" Click="btnOK1_Click" Width="50" Height="25" Margin="5" />
      <Button Content="取消" Click="btnCancel1_Click" Width="50" Height="25" Margin="5"/>
     </StackPanel>
    </StackPanel>
   </Label>
  </Popup>
  <Popup Name="pop2" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Fade" PlacementTarget="{Binding ElementName=btnTestPopup2}" Placement="Top" >
   <Label Style="{StaticResource BottomBubblePanel}" Tag="Top">
    <StackPanel>
     <StackPanel Orientation="Horizontal">
      <TextBlock Text="abc" />
      <TextBox Width="80" Name="txtTest2" />
     </StackPanel>
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
      <Button Content="确定" Click="btnOK2_Click" Width="50" Height="25" Margin="5"/>
      <Button Content="取消" Click="btnCancel2_Click" Width="50" Height="25" Margin="5"/>
     </StackPanel>
    </StackPanel>
   </Label>
  </Popup>
  <Popup Name="pop3" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Scroll" PlacementTarget="{Binding ElementName=btnTestPopup3}" Placement="Right" >
   <Label Style="{StaticResource LeftBubblePanel}" Tag="Top">
    <StackPanel>
     <StackPanel Orientation="Horizontal">
      <TextBlock Text="abc" />
      <TextBox Width="80" Name="txtTest3" />
     </StackPanel>
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
      <Button Content="确定" Click="btnOK2_Click" Width="50" Height="25" Margin="5"/>
      <Button Content="取消" Click="btnCancel3_Click" Width="50" Height="25" Margin="5"/>
     </StackPanel>
    </StackPanel>
   </Label>
  </Popup>
  <Popup Name="pop4" AllowsTransparency="True" StaysOpen="False" PopupAnimation="None" PlacementTarget="{Binding ElementName=btnTestPopup4}" Placement="Left" >
   <Label Style="{StaticResource RightBubblePanel}" Tag="Top">
    <StackPanel>
     <StackPanel Orientation="Horizontal">
      <TextBlock Text="abc" />
      <TextBox Width="80" Name="txtTest4" />
     </StackPanel>
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
      <Button Content="确定" Click="btnOK4_Click" Width="50" Height="25" Margin="5"/>
      <Button Content="取消" Click="btnCancel4_Click" Width="50" Height="25" Margin="5"/>
     </StackPanel>
    </StackPanel>
   </Label>
  </Popup>
 </StackPanel>
</Window>

后台代码,很简单,就是控制pupup显示或隐藏

private void btnTestPopup1_Click(object sender, RoutedEventArgs e)
  {
   pop1.IsOpen = true;
  }
  private void btnOK1_Click(object sender, RoutedEventArgs e)
  {
   pop1.IsOpen = false;
  }
  private void btnCancel1_Click(object sender, RoutedEventArgs e)
  {
   pop1.IsOpen = false;
  }

  private void btnTestPopup2_Click(object sender, RoutedEventArgs e)
  {
   pop2.IsOpen = true;
  }
  private void btnOK2_Click(object sender, RoutedEventArgs e)
  {
   pop2.IsOpen = false;
  }
  private void btnCancel2_Click(object sender, RoutedEventArgs e)
  {
   pop2.IsOpen = false;
  }

  private void btnTestPopup3_Click(object sender, RoutedEventArgs e)
  {
   pop3.IsOpen = true;
  }
  private void btnOK3_Click(object sender, RoutedEventArgs e)
  {
   pop3.IsOpen = false;
  }
  private void btnCancel3_Click(object sender, RoutedEventArgs e)
  {
   pop3.IsOpen = false;
  }

  private void btnTestPopup4_Click(object sender, RoutedEventArgs e)
  {
   pop4.IsOpen = true;
  }
  private void btnOK4_Click(object sender, RoutedEventArgs e)
  {
   pop4.IsOpen = false;
  }
  private void btnCancel4_Click(object sender, RoutedEventArgs e)
  {
   pop4.IsOpen = false;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Entity Framework Core更新时间映射

    Entity Framework Core更新时间映射

    这篇文章介绍了Entity Framework Core更新时间映射的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.net处理XML数据实例浅析

    ASP.net处理XML数据实例浅析

    这篇文章主要介绍了ASP.net处理XML数据实例浅析,分析了XML的原理与用法,并以实例形式讲述了asp.net处理XML数据的方法,需要的朋友可以参考下
    2014-10-10
  • 国产化之Arm64 CPU+银河麒麟系统安装.NetCore的步骤详解

    国产化之Arm64 CPU+银河麒麟系统安装.NetCore的步骤详解

    这篇文章主要介绍了国产化之Arm64 CPU+银河麒麟系统安装.NetCore,这里就以ARM架构举例,其它CPU平台的安装过程都一样,要下载的包不同而已,感兴趣的朋友跟随小编一起看看吧
    2022-03-03
  • .Net使用Xunit工具进行单元测试

    .Net使用Xunit工具进行单元测试

    这篇文章介绍了.Net使用Xunit工具进行单元测试的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • .Net中的序列化和反序列化详解

    .Net中的序列化和反序列化详解

    这篇文章主要介绍了.Net中的序列化和反序列化详解的相关资料,需要的朋友可以参考下
    2016-04-04
  • asp.net 无刷新翻页就是这么简单

    asp.net 无刷新翻页就是这么简单

    前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
    2010-03-03
  • ASP.NET中的几种弹出框提示基本实现方法

    ASP.NET中的几种弹出框提示基本实现方法

    NET程序的开发过程中,常常需要和用户进行信息交互,对话框的出现将解决了这些问题,下面是本人对常用对话框使用的小结,希望对大家有所帮助
    2013-03-03
  • asp.net关于onpropertychange和oninput事件实现代码

    asp.net关于onpropertychange和oninput事件实现代码

    文本框,数据列表,当在文本框中输入条件时需要实时刷新数据列表,而且需要满足多浏览器(IE6.0/7.0/8.0,FireFox,Opera,google chrome,Safair)其功能类似google的智能匹配,我是用asp.net实现的。
    2009-11-11
  • 关于.NET Attribute在数据校验中的应用教程

    关于.NET Attribute在数据校验中的应用教程

    这篇文章主要给大家介绍了关于.NET Attribute在数据校验中的应用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • asp.net实现文件无刷新上传方法汇总

    asp.net实现文件无刷新上传方法汇总

    本文给大家介绍的是asp.net实现文件无刷新上传的2种方法,分别是使用swfupload插件和uploadify插件,讲述的十分细致全面,附上示例,有需要的小伙伴可以参考下。
    2015-06-06

最新评论