WPF界面布局相关操作方法介绍

WPF开发工具之所以从出现至今一直深受广大开发人员的青睐,得益于其强大的图形界面处理功能。可以轻松方便的使我们创造出精美的图形。WPF界面布局是其中比较基础的一个知识点。#t#

传统的应用程序平台(如 Win32)几乎没有布局的概念:控件放置在画布上的 (x,y) 坐标系中,并且开发人员需要手动提供对确定任何元素的原点和尺寸的支持(考虑窗口大小调整和显示器 DPI 设置)。

另一方面,Windows Presentation Foundation 提供多种适合于内容并且在窗口内管理控件和项目位置的布局实现。

下面的代码直接拷贝到XamlPad中就可以看到效果(XamlPad在什么地方?安装Framework3.0 SDK就可以了)

WPF界面布局示例1:

  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"
     > 
  2. < Grid> 
  3. < Image Source="C:\WINDOWS\Web\
    Wallpaper\follow.jpg"
     /> 
  4. < Button Width="100" Height="50" 
    Content="这就是一个测试" /> 
  5. < /Grid> 
  6. < /Page> 

 

WPF界面布局示例2:

 
 
 
  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"
     > 
  2. < Grid> 
  3. < Button Width="400" Height="300" > 
  4. < Image Source="C:\WINDOWS\Web\
    Wallpaper\follow.jpg"
     /> 
  5. < /Button> 
  6. < /Grid> 
  7. < /Page> 

Button 可按上面方式包含图像,Button成为Image控件的宿主。

此外,Windows Presentation Foundation 提供五个WPF界面布局面板,以便控制和约束子元素的大小和位置:Canvas、DockPanel、StackPanel Grid 和 WrapPanel。

THE END