WPF - 彈出視窗



Popup 是一種控制元件,用於在應用程式視窗範圍內,現有內容的頂部顯示內容。它是在其他內容上進行的臨時顯示。Popup 類的層次繼承如下:

Hierarchical of Popup

Popup 類常用屬性

序號 屬性及說明
1

Child

獲取或設定要在彈出視窗中託管的內容。

2

ChildProperty

獲取 Child 依賴屬性的識別符號。

3

ChildTransitions

獲取或設定應用於 Popup 子內容的 Transition 樣式元素的集合。

4

ChildTransitionsProperty

標識 ChildTransitions 依賴屬性。

5

HorizontalOffset

獲取或設定應用程式視窗左側與彈出視窗左側之間的距離。

6

HorizontalOffsetProperty

獲取 HorizontalOffset 依賴屬性的識別符號。

7

IsLightDismissEnabled

獲取或設定一個值,該值確定如何關閉 Popup。

8

IsLightDismissEnabledProperty

標識 IsLightDismissEnabled 依賴屬性。

9

IsOpen

獲取或設定彈出視窗當前是否顯示在螢幕上。

10

IsOpenProperty

獲取 IsOpen 依賴屬性的識別符號。

11

VerticalOffset

獲取或設定應用程式視窗頂部與彈出視窗頂部之間的距離。

12

VerticalOffsetProperty

獲取 VerticalOffset 依賴屬性的識別符號。

Popup 類常用事件

序號 事件及說明
1

Closed

當 IsOpen 屬性設定為 false 時觸發。

2

Opened

當 IsOpen 屬性設定為 true 時觸發。

示例

  • 讓我們建立一個名為 WPFPopupControl 的新 WPF 專案。

  • 當您檢視工具箱時,會發現沒有彈出視窗控制元件。但是您可以從 XAML 中嚮應用程式新增彈出視窗控制元件。

  • 以下示例演示瞭如何使用 Popup 控制元件。這是一個 XAML 程式碼示例,其中建立並初始化了一個 Popup 控制元件和一個 CheckBox。當用戶選中 CheckBox 時,它會顯示一個 Popup。

<Window x:Class = "WPFPopupControl.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFPopupControl" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <CheckBox Name = "PCheckBox" Margin = "198,94,208,194" Content = "Checked Me" /> 
      <Popup IsOpen = "{Binding ElementName = PCheckBox,Path = IsChecked}"    
         PlacementTarget = "{Binding ElementName = PCheckBox}"
         AllowsTransparency = "True" PopupAnimation = "Slide"> 
			
         <Canvas Width = "125" Height = "100" Background = "LightGray"> 
            <Canvas.RenderTransform>
               <RotateTransform x:Name = "theTransform" /> 
            </Canvas.RenderTransform> 
            <TextBlock TextWrapping = "Wrap" Foreground = "Blue"
               Text = "Hi, this is Popup" /> 
         </Canvas> 
			
      </Popup> 
   </Grid> 
	
</Window>

編譯並執行上述程式碼後,將產生以下輸出。選中複選框時,將出現一個彈出視窗;取消選中複選框時,彈出視窗將消失。

Output of Popup

我們建議您執行上述示例程式碼並嘗試 Popup 類的其他屬性和事件。

wpf_controls.htm
廣告

© . All rights reserved.