WPF - 網格面板



網格面板提供了一個靈活的區域,該區域由行和列組成。在網格中,子元素可以以表格形式排列。可以使用Grid.RowGrid.Column屬性將元素新增到任何特定的行和列。

預設情況下,網格面板建立一個一行一列的網格。多行多列透過RowDefinitions和ColumnDefinitions屬性建立。行的高度和列的寬度可以透過以下三種方式定義:

  • 固定值 - 為邏輯單位(1/96英寸)分配固定大小。

  • 自動 - 它將佔用該特定行/列中控制元件所需的空間。

  • 星號 (*) - 當自動和固定大小的空間被填充後,它將佔用剩餘的空間。

Canvas類的層次繼承如下。

Hierarchical of GridPanel

Grid 類常用屬性

序號 屬性及說明
1

Background

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

2

Children

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

3

ColumnDefinitions

獲取在此 Grid 例項上定義的 ColumnDefinition 物件列表。

4

Height

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

5

ItemHeight

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

6

ItemWidth

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

7

Margin

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

8

Name

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

9

Orientation

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

10

Parent

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

11

Resources

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

12

RowDefinitions

獲取在此 Grid 例項上定義的 RowDefinition 物件列表。

13

Style

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

14

Width

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

Grid 類常用方法

序號 方法及說明
1

GetColumn

從指定的 FrameworkElement 獲取 Grid.Column XAML 附加屬性的值。

2

GetColumnSpan

從指定的 FrameworkElement 獲取 Grid.ColumnSpan XAML 附加屬性的值。

3

GetRow

從指定的 FrameworkElement 獲取 Grid.Row XAML 附加屬性的值。

4

SetColumn

設定指定的 FrameworkElement 上 Grid.Column XAML 附加屬性的值。

5

SetRow

設定指定的 FrameworkElement 上 Grid.Row XAML 附加屬性的值。

6

SetRowSpan

設定指定的 FrameworkElement 上 Grid.RowSpan XAML 附加屬性的值。

示例

以下示例演示如何將子元素新增到 Grid 以指定表格形式。在以下 XAML 實現中,文字塊新增到 Grid 的第一列,文字框新增到 Grid 的第二列。

<Window x:Class = "WPFGrid.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:WPFGrid" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid x:Name = "FormLayoutGrid" Background = "AliceBlue"> 
      <Grid.ColumnDefinitions> 
         <ColumnDefinition Width = "Auto" /> 
         <ColumnDefinition />
      </Grid.ColumnDefinitions> 
		
      <Grid.RowDefinitions> 
         <RowDefinition Height = "*" /> 
         <RowDefinition Height = "*" /> 
         <RowDefinition Height = "*" /> 
      </Grid.RowDefinitions> 
		
      <TextBlock Grid.Row = "0" Grid.Column = "0" Text = "Name" Margin = "10"  
         HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" /> 
      <TextBox Grid.Row = "0" Grid.Column = "1" Margin = "10" /> 
      <TextBlock Grid.Row = "1" Grid.Column = "0" Text = "ID" Margin = "10"  
         HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" /> 
      <TextBox Grid.Row = "1" Grid.Column = "1" Margin = "10" /> 
      <TextBlock Grid.Row = "2" Grid.Column = "0" Text = "Age" Margin = "10"  
         HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" /> 
      <TextBox Grid.Row = "2" Grid.Column = "1" Margin = "10" /> 
   </Grid> 
	
</Window>

編譯並執行上述程式碼後,將生成以下視窗。

Output of Gridpanel

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

wpf_layouts.htm
廣告
© . All rights reserved.