WPF - 堆疊面板



堆疊面板 (Stack panel) 是 XAML 中一個簡單而有用的佈局面板。在堆疊面板中,子元素可以根據 orientation 屬性沿水平或垂直方向排列成單行。它常用於建立任何型別的列表。StackPanel 類的層次繼承如下:

Hierarchical of Stackpanel

StackPanel 常用屬性

序號 屬性和描述
1

Background (背景)

獲取或設定填充面板內容區域的 Brush。(繼承自 Panel)

2

Children (子元素)

獲取此 Panel 的子元素的 UIElementCollection。(繼承自 Panel。)

3

Height (高度)

獲取或設定元素的建議高度。(繼承自 FrameworkElement。)

4

ItemHeight (專案高度)

獲取或設定一個值,該值指定 WrapPanel 中包含的所有專案的 height。

5

ItemWidth (專案寬度)

獲取或設定一個值,該值指定 WrapPanel 中包含的所有專案的 width。

6

LogicalChildren (邏輯子元素)

獲取一個列舉器,該列舉器可以迭代此 Panel 元素的邏輯子元素。(繼承自 Panel。)

7

LogicalOrientation (邏輯方向)

如果面板僅支援在一個維度上進行佈局,則為面板的方向。(繼承自 Panel。)

8

Margin (邊距)

獲取或設定元素的外邊距。(繼承自 FrameworkElement。)

9

Name (名稱)

獲取或設定元素的標識名稱。該名稱提供一個引用,以便程式碼隱藏(例如事件處理程式程式碼)可以在 XAML 處理器在處理過程中構造標記元素後引用它。(繼承自 FrameworkElement。)

10

Orientation (方向)

獲取或設定一個值,該值指定排列子內容的維度。

11

Parent (父元素)

獲取此元素的邏輯父元素。(繼承自 FrameworkElement。)

12

Resources (資源)

獲取或設定區域性定義的資源字典。(繼承自 FrameworkElement。)

13

Style (樣式)

獲取或設定此元素呈現時使用的樣式。(繼承自 FrameworkElement。)

14

Width (寬度)

獲取或設定元素的寬度。(繼承自 FrameworkElement。)

示例

下面的示例演示如何將子元素新增到 StackPanel。以下 XAML 實現建立了具有某些屬性的 StackPanel 內的按鈕。

<Window x:Class = "WPFStackPanel.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:WPFStackPanel" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid> 
      <StackPanel Orientation = "Horizontal"> 
         <Button x:Name = "button" Content = "Button" Margin = "10" Width = "120" Height = "30" /> 
         <Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" /> 
         <Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" /> 
         <Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" /> 
      </StackPanel>  
   </Grid> 
	
</Window> 

編譯並執行上述程式碼後,將生成以下視窗。您可以看到子元素按水平順序排列。您可以透過將 orientation 屬性設定為 Horizontal 來更改排列方式。預設情況下,子元素將按垂直順序排列。

Output of Stackpanel

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

wpf_layouts.htm
廣告
© . All rights reserved.