WPF界面布局相關操作方法介紹
WPF開發工具之所以從出現至今一直深受廣大開發人員的青睞,得益于其強大的圖形界面處理功能。可以輕松方便的使我們創造出精美的圖形。WPF界面布局是其中比較基礎的一個知識點。#t#
傳統的應用程序平臺(如 Win32)幾乎沒有布局的概念:控件放置在畫布上的 (x,y) 坐標系中,并且開發人員需要手動提供對確定任何元素的原點和尺寸的支持(考慮窗口大小調整和顯示器 DPI 設置)。
另一方面,Windows Presentation Foundation 提供多種適合于內容并且在窗口內管理控件和項目位置的布局實現。
下面的代碼直接拷貝到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="這就是一個測試" /> - < /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 提供五個WPF界面布局面板,以便控制和約束子元素的大小和位置:Canvas、DockPanel、StackPanel Grid 和 WrapPanel。