WPF界面布局相關(guān)操作方法介紹
WPF開發(fā)工具之所以從出現(xiàn)至今一直深受廣大開發(fā)人員的青睞,得益于其強(qiáng)大的圖形界面處理功能??梢暂p松方便的使我們創(chuàng)造出精美的圖形。WPF界面布局是其中比較基礎(chǔ)的一個(gè)知識(shí)點(diǎn)。#t#
傳統(tǒng)的應(yīng)用程序平臺(tái)(如 Win32)幾乎沒有布局的概念:控件放置在畫布上的 (x,y) 坐標(biāo)系中,并且開發(fā)人員需要手動(dòng)提供對(duì)確定任何元素的原點(diǎn)和尺寸的支持(考慮窗口大小調(diào)整和顯示器 DPI 設(shè)置)。
另一方面,Windows Presentation Foundation 提供多種適合于內(nèi)容并且在窗口內(nèi)管理控件和項(xiàng)目位置的布局實(shí)現(xiàn)。
下面的代碼直接拷貝到XamlPad中就可以看到效果(XamlPad在什么地方?安裝Framework3.0 SDK就可以了)
WPF界面布局示例1:
- < Page xmlns="http://schemas.
microsoft.com/winfx/2006/xaml/
presentation" xmlns:sys=
"clr-namespace:System;assembly=
mscorlib" xmlns:x="http://
schemas.microsoft.com/winfx/2006/xaml" > - < Grid>
- < Image Source="C:\WINDOWS\Web\
Wallpaper\follow.jpg" /> - < Button Width="100" Height="50"
Content="這就是一個(gè)測(cè)試" /> - < /Grid>
- < /Page>
WPF界面布局示例2:
- < Page xmlns="http://schemas.
microsoft.com/winfx/2006/xaml/
presentation" xmlns:sys="clr-
namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.
com/winfx/2006/xaml" >- < Grid>
- < Button Width="400" Height="300" >
- < Image Source="C:\WINDOWS\Web\
Wallpaper\follow.jpg" />- < /Button>
- < /Grid>
- < /Page>
Button 可按上面方式包含圖像,Button成為Image控件的宿主。
此外,Windows Presentation Foundation 提供五個(gè)WPF界面布局面板,以便控制和約束子元素的大小和位置:Canvas、DockPanel、StackPanel Grid 和 WrapPanel。