WPF氣泡樣式彈窗效果

来源:http://www.cnblogs.com/Amiry/archive/2016/09/02/5832815.html
-Advertisement-
Play Games

頁面設計需求,做了一個氣泡形狀的彈出框,效果如下: 設計思路如下: 1. 使用Path繪製氣泡的尖尖,將這個放到頂層; 2. 在用border繪製長方形框,將這個放到底層,並且設置Margin值,使得Path圖層和border看起來銜接在一起。 代碼如下: 後臺代碼,很簡單,就是控制pupup顯示或 ...


頁面設計需求,做了一個氣泡形狀的彈出框,效果如下:

設計思路如下:

        1. 使用Path繪製氣泡的尖尖,將這個放到頂層;

        2. 在用border繪製長方形框,將這個放到底層,並且設置Margin值,使得Path圖層和border看起來銜接在一起。

代碼如下:

  1 <Window x:Class="BubblePanelTest.MainWindow"
  2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4         Title="MainWindow" Height="350" Width="525">
  5     <Window.Resources>
  6         <Style TargetType="Label" x:Key="TopBubblePanel">
  7             <Setter Property="Template">
  8                 <Setter.Value>
  9                     <ControlTemplate TargetType="{x:Type Label}">
 10                         <Grid>
 11                             <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Top"  Background="Yellow" HorizontalAlignment="Left" Margin="0,8.5,0,0" Padding="5">
 12                                 <ContentPresenter />
 13                             </Border>
 14                             <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" Background="Transparent">
 15                                 <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
 16                                     <Path.Data>
 17                                         <PathGeometry Figures="M 0,10
 18                                   L 0,10,5,0
 19                                   L 5,0,10,10
 20                                   "/>
 21                                     </Path.Data>
 22                                 </Path>
 23                             </Canvas>
 24                         </Grid>
 25                     </ControlTemplate>
 26                 </Setter.Value>
 27             </Setter>
 28         </Style>
 29         <Style TargetType="Label" x:Key="BottomBubblePanel">
 30             <Setter Property="Template">
 31                 <Setter.Value>
 32                     <ControlTemplate TargetType="{x:Type Label}">
 33                         <Grid>
 34                             <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Bottom" Margin="0,0,0,8.5" Background="Yellow" HorizontalAlignment="Left" Padding="5">
 35                                 <ContentPresenter />
 36                             </Border>
 37                             <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10,0,0,0" Background="Transparent">
 38                                 <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
 39                                     <Path.Data>
 40                                         <PathGeometry Figures="M 0,0
 41                                   L 0,0,5,10
 42                                   L 5,10,10,0
 43                                   "/>
 44                                     </Path.Data>
 45                                 </Path>
 46                             </Canvas>
 47                         </Grid>
 48                     </ControlTemplate>
 49                 </Setter.Value>
 50             </Setter>
 51         </Style>
 52         <Style TargetType="Label" x:Key="LeftBubblePanel">
 53             <Setter Property="Template">
 54                 <Setter.Value>
 55                     <ControlTemplate TargetType="{x:Type Label}">
 56                         <Grid>
 57                             <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Top" Margin="8.5,0,0,0" Background="Yellow" HorizontalAlignment="Left" Padding="5">
 58                                 <ContentPresenter />
 59                             </Border>
 60                             <Canvas Width="10" Height="10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" Background="Transparent">
 61                                 <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
 62                                     <Path.Data>
 63                                         <PathGeometry Figures="M 10,0
 64                                   L 10,0,0,5
 65                                   L 0,5,10,10
 66                                   "/>
 67                                     </Path.Data>
 68                                 </Path>
 69                             </Canvas>
 70                         </Grid>
 71                     </ControlTemplate>
 72                 </Setter.Value>
 73             </Setter>
 74         </Style>
 75         <Style TargetType="Label" x:Key="RightBubblePanel">
 76             <Setter Property="Template">
 77                 <Setter.Value>
 78                     <ControlTemplate TargetType="{x:Type Label}">
 79                         <Grid HorizontalAlignment="Left">
 80                             <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,8.5,0" Background="Yellow" Padding="5">
 81                                 <ContentPresenter />
 82                             </Border>
 83                             <Canvas Width="10" Height="10" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,0,0" Background="Transparent">
 84                                 <Path Stroke="Black" StrokeThickness="0.5" Fill="Yellow">
 85                                     <Path.Data>
 86                                         <PathGeometry Figures="M 0,0
 87                                   L 0,0,10,5
 88                                   L 10,5,0,10
 89                                   "/>
 90                                     </Path.Data>
 91                                 </Path>
 92                             </Canvas>
 93                         </Grid>
 94                     </ControlTemplate>
 95                 </Setter.Value>
 96             </Setter>
 97         </Style>
 98     </Window.Resources>
 99     <StackPanel>
100         <Label Style="{StaticResource TopBubblePanel}" Tag="Top" Margin="2">
101             <StackPanel>
102                 <StackPanel Orientation="Horizontal">
103                     <TextBlock Text="abc" />
104                     <TextBox Width="80"/>
105                 </StackPanel>
106             </StackPanel>
107         </Label>
108         <Label Style="{StaticResource BottomBubblePanel}" Tag="Top" Margin="2">
109             <StackPanel>
110                 <StackPanel Orientation="Horizontal">
111                     <TextBlock Text="abc" />
112                     <TextBox Width="80"/>
113                 </StackPanel>
114             </StackPanel>
115         </Label>
116         <Label Style="{StaticResource LeftBubblePanel}" Tag="Top" Margin="2">
117             <StackPanel>
118                 <StackPanel Orientation="Horizontal">
119                     <TextBlock Text="abc" />
120                     <TextBox Width="80"/>
121                 </StackPanel>
122             </StackPanel>
123         </Label>
124         <Label Style="{StaticResource RightBubblePanel}" Tag="Top" Margin="2">
125             <StackPanel>
126                 <StackPanel Orientation="Horizontal">
127                     <TextBlock Text="abc" />
128                     <TextBox Width="80"/>
129                 </StackPanel>
130             </StackPanel>
131         </Label>
132         <StackPanel Orientation="Horizontal" Margin="0,30,0,0">
133             <Button Name="btnTestPopup1" Width="100" Height="30" Content="Bottom" Click="btnTestPopup1_Click" />
134             <Button Name="btnTestPopup2" Width="100" Height="30" Content="Top" Click="btnTestPopup2_Click" />
135             <Button Name="btnTestPopup3" Width="100" Height="30" Content="Right" Click="btnTestPopup3_Click" />
136             <Button Name="btnTestPopup4" Width="100" Height="30" Content="Left" Click="btnTestPopup4_Click" />
137         </StackPanel>
138         <Popup Name="pop1" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=btnTestPopup1}" Placement="Bottom" >
139             <Label Style="{StaticResource TopBubblePanel}" Tag="Top">
140                 <StackPanel>
141                     <StackPanel Orientation="Horizontal">
142                         <TextBlock Text="abc" />
143                         <TextBox Width="80" Name="txtTest1" />
144                     </StackPanel>
145                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
146                         <Button Content="確定" Click="btnOK1_Click" Width="50" Height="25" Margin="5" />
147                         <Button Content="取消" Click="btnCancel1_Click"  Width="50" Height="25" Margin="5"/>
148                     </StackPanel>
149                 </StackPanel>
150             </Label>
151         </Popup>
152         <Popup Name="pop2" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Fade"  PlacementTarget="{Binding ElementName=btnTestPopup2}" Placement="Top" >
153             <Label Style="{StaticResource BottomBubblePanel}" Tag="Top">
154                 <StackPanel>
155                     <StackPanel Orientation="Horizontal">
156                         <TextBlock Text="abc" />
157                         <TextBox Width="80" Name="txtTest2" />
158                     </StackPanel>
159                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
160                         <Button Content="確定" Click="btnOK2_Click"  Width="50" Height="25" Margin="5"/>
161                         <Button Content="取消" Click="btnCancel2_Click"  Width="50" Height="25" Margin="5"/>
162                     </StackPanel>
163                 </StackPanel>
164             </Label>
165         </Popup>
166         <Popup Name="pop3" AllowsTransparency="True" StaysOpen="False" PopupAnimation="Scroll"  PlacementTarget="{Binding ElementName=btnTestPopup3}" Placement="Right" >
167             <Label Style="{StaticResource LeftBubblePanel}" Tag="Top">
168                 <StackPanel>
169                     <StackPanel Orientation="Horizontal">
170                         <TextBlock Text="abc" />
171                         <TextBox Width="80" Name="txtTest3" />
172                     </StackPanel>
173                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
174                         <Button Content="確定" Click="btnOK2_Click"  Width="50" Height="25" Margin="5"/>
175                         <Button Content="取消" Click="btnCancel3_Click"  Width="50" Height="25" Margin="5"/>
176                     </StackPanel>
177                 </StackPanel>
178             </Label>
179         </Popup>
180         <Popup Name="pop4" AllowsTransparency="True" StaysOpen="False" PopupAnimation="None" PlacementTarget="{Binding ElementName=btnTestPopup4}" Placement="Left" >
181             <Label Style="{StaticResource RightBubblePanel}" Tag="Top">
182                 <StackPanel>
183                     <StackPanel Orientation="Horizontal">
184                         <TextBlock Text="abc" />
185                         <TextBox Width="80" Name="txtTest4" />
186                     </StackPanel>
187                     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
188                         <Button Content="確定" Click="btnOK4_Click"  Width="50" Height="25" Margin="5"/>
189                         <Button Content="取消" Click="btnCancel4_Click"  Width="50" Height="25" Margin="5"/>
190                     </StackPanel>
191                 </StackPanel>
192             </Label>
193         </Popup>
194     </StackPanel>
195 </Window>

後臺代碼,很簡單,就是控制pupup顯示或隱藏

 1 private void btnTestPopup1_Click(object sender, RoutedEventArgs e)
 2         {
 3             pop1.IsOpen = true;
 4         }
 5         private void btnOK1_Click(object sender, RoutedEventArgs e)
 6         {
 7             pop1.IsOpen = false;
 8         }
 9         private void btnCancel1_Click(object sender, RoutedEventArgs e)
10         {
11             pop1.IsOpen = false;
12         }
13 
14         private void btnTestPopup2_Click(object sender, RoutedEventArgs e)
15         {
16             pop2.IsOpen = true;
17         }
18         private void btnOK2_Click(object sender, RoutedEventArgs e)
19         {
20             pop2.IsOpen = false;
21         }
22         private void btnCancel2_Click(object sender, RoutedEventArgs e)
23         {
24             pop2.IsOpen = false;
25         }
26 
27         private void btnTestPopup3_Click(object sender, RoutedEventArgs e)
28         {
29             pop3.IsOpen = true;
30         }
31         private void btnOK3_Click(object sender, RoutedEventArgs e)
32         {
33             pop3.IsOpen = false;
34         }
35         private void btnCancel3_Click(object sender, RoutedEventArgs e)
36         {
37             pop3.IsOpen = false;
38         }
39 
40         private void btnTestPopup4_Click(object sender, RoutedEventArgs e)
	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • LVS介紹 lvs 核心ipvs Ipvs(IP Virtual Server)是整個負載均衡的基礎,如果沒有這個基礎,故障隔離與失敗切換就毫無意義了。Ipvs 具體實現是由ipvsadm 這個程式來完成,因此判斷一個系統是否具備ipvs 功能,只需要察看ipvsadm 程式是否被安裝。察看ipvs ...
  • 1. 安裝libevent 2. 安裝memcached 3. 安裝memagent 3-1。修改Makefile 3-2。修改ketama.h 3-3.安裝memagent 1 make 2 ln -i /usr/local/magent/magent /usr/bin/magent 4. 使用m ...
  • 1.U-Boot,全稱 Universal Boot Loader,是遵循GPL條款的開放源碼項目。U-Boot的作用是系統引導。U-Boot從FADSROM、8xxROM、PPCBOOT逐步發展演化而來。其源碼目錄、編譯形式與Linux內核很相似,事實上,不少U-Boot源碼就是根據相應的Linu ...
  • ...
  • There are commonly three types of memories in a PIC Microcontroller, Flash Program Memory, Data Memory (RAM) and EEPROM Data Memory. We write Programs... ...
  • Ubuntu 16.04系統在一開始安裝完成時是無法切換到 root 用戶的,普通用戶需要使用 root 許可權的時候通常需要在執行命令前加 "sudo",需要經常使用root許可權的伙伴可能會覺得這會讓我們的蛋蛋同學很憂傷...... 其實,要解決蛋蛋同學的問題很簡單,只要給 root 配一個密碼即可 ...
  • 整理Apache+Mysql+PHP+PHPWind(Apache+PHP集成環境) 一、情況簡述: 1、虛擬機VM上面CentOS 2、全部yum安裝(yum安裝與源碼安裝的安裝路徑不同) 二、操作步驟簡述 安裝Apache(httpd) 安裝Mysql(mysqld) 安裝PHP(phpd-fd ...
  • 使用HttpListener實現簡單的Http服務 HttpListener提供一個簡單的、可通過編程方式控制的 HTTP 協議偵聽器.使用它可以很容易的提供一些Http服務,而無需啟動IIS這類大型服務程式。使用HttpListener的方法流程很簡單:主要分為以下幾步 創建一個HTTP偵聽器對象 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...