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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...