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;
  }

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索WPF气泡样式弹窗
, WPF气泡弹窗
, WPF弹窗
WPF气泡
wpf tooltip 气泡样式、气泡弹窗、android 气泡弹窗、wpf 弹窗、wpf 气泡提示框,以便于您获取更多的相关知识。

时间: 2024-09-22 11:28:31

WPF气泡样式弹窗效果代码分享_实用技巧的相关文章

WPF自定义搜索框代码分享_实用技巧

首先下载搜索图标: 控件中的搜索图标下载地址:http://www.easyicon.net/1183666-Search_icon.html  搜索框设计过程比较简单:  1.先定义一个Rectangle作为背景  2.然后中间放TextBox输入,可以重写其中的模板.提示语Label放在模板中,可以在模板的触发器中控制隐藏显示~  3.搜索按钮-大家随便在网上下个就行了.  UserControl界面:  <UserControl x:Class="WpfApplication18.S

Asp.Net类型转换类(通用类)代码分享_实用技巧

废话不多说了,直接给大家贴代码了,具体代码如下所述: /// <summary> /// 类型转换类 /// 处理数据库获取字段为空的情况 /// </summary> public static class DBConvert { #region------------------ToInt32类型转换------------------ /// <summary> /// 读取数据库中字符串并转换成Int32 /// 为空时返回0 /// </summary&

动态生成table并实现分页效果心得分享_实用技巧

前台代码: 复制代码 代码如下: <table style="width: 100%;"> <tr> <td> 搜索字: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="搜索&

asp.net微软图表控件使用示例代码分享_实用技巧

复制代码 代码如下: <configuration>  <system.webServer>    <handlers>      <remove name="ChartImageHandler" />      <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" 

ASP.NET餐饮管理系统制作代码分享_实用技巧

页面介绍展示: 以上是餐饮管理系统制作图片介绍,接下来是代码部分. menu.aspx  <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="menu.aspx.cs" Inherits="menu" Title="无标题页" %> <

asp.net+ajaxfileupload.js 实现文件异步上传代码分享_实用技巧

由于代码很简单,这里就闲话不多说了,直接上代码,小伙伴们自己研读代码就明白了. 前台代码:  复制代码 代码如下: /*修改头像*/      //上传      function _sc() {          $(".ckfile").html("").css("color", "#535353");          $("#_userImgPath").val("");    

asp.net导出Excel类库代码分享_实用技巧

复制代码 代码如下: using System;using System.Collections.Generic;using System.Reflection;using System.Web;using Excel = Microsoft.Office.Interop.Excel; /// <summary>///ExcelClass 的摘要说明/// </summary>public class ExcelClass{    /// <summary>    //

.net重启iis线程池和iis站点程序代码分享_实用技巧

重启站点: 复制代码 代码如下:  /// <summary>        /// 根据名字重启站点.(没重启线程池)        /// </summary>        /// <param name="sitename"></param>        static void RestartWEbSite(string sitename)        {            try            {        

.net socket客户端实例代码分享_实用技巧

客户端代码 复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Net; using System.Threading; using System.Net.Sockets;   namespace W.Common {     public class CacheSocket     {