WPF筆記(1.4 布局)——Hello,WPF!

來源:互聯網
上載者:User

標籤:

原文:WPF筆記(1.4 布局)——Hello,WPF!

這一節只是第2章的引子。
布局要使用Panel控制項,有四種Panel,如下:
DockPanel,就是設定固定位置布局模型。
StackPanel,提供一個從左至右或從上至下放置內容的堆棧模型。
Grid,提供一個允許進行 行/網格定位的模型。可使用表格。
Canvas,可精確定位。

其中,Grid是最常用的,vs2005自動產生的Page和window都預設帶有這個標籤:

Example 1-25. A sample usage of the Grid panel
<Window >
  
<Grid>
    
<Grid.RowDefinitions>
      
<RowDefinition />
      
<RowDefinition />
      
<RowDefinition />
    
</Grid.RowDefinitions>
    
<Grid.ColumnDefinitions>
      
<ColumnDefinition />
      
<ColumnDefinition />
      
<ColumnDefinition />
    
</Grid.ColumnDefinitions>
    
<Button Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">A</Button>
    
<Button Grid.Row="0" Grid.Column="2">C</Button>
    
<Button Grid.Row="1" Grid.Column="0" Grid.RowSpan="2">D</Button>
    
<Button Grid.Row="1" Grid.Column="1">E</Button>
    
<Button Grid.Row="1" Grid.Column="2">F</Button>
    
<Button Grid.Row="2" Grid.Column="1">H</Button>
    
<Button Grid.Row="2" Grid.Column="2">I</Button>
  
</Grid>
</Window>這段程式產生一個3x3表格。注意,先定義行格式,再定義列格式,最後是往儲存格放入button。

Example 1-26. Arranging an image and text in a grid
<Button Width="100" Height="100">
  
<Button.Content>
    
<Grid>
      
<Grid.RowDefinitions>
        
<RowDefinition />
        
<RowDefinition />
      
</Grid.RowDefinitions>
      
<Image Grid.Row="0" Source="tom.png" />
      
<TextBlock
        Grid.Row
="1"
        HorizontalAlignment
="Center">Tom</TextBlock>
    
</Grid>
  
</Button.Content>
</Button>這段程式是在圖片下面加了一行Caption,也是用的Grid下表格排版。

以上兩個例子都有Grid.Row=1這樣的文法——attached-property牽連屬性。即在Grid內部定義(),在外部控制項Button中指定屬性值。
牽連屬性的用途,事先不一定用Button填充儲存格,這樣對所有控制項就有任意性——暫時這麼想,因為沒看第二章。

WPF筆記(1.4 布局)——Hello,WPF!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.