[Net_Ghost] WPF实例教程(4)

代码所在位置:WPFSamples/Intro/QuickStart3

 

Page1.xaml

<Page

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <DockPanel    

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <TextBlock Background="LightBlue"

                DockPanel.Dock="Top">Some Text</TextBlock>

    <TextBlock DockPanel.Dock="Bottom"

                Background="LightYellow">Some text at the bottom of the page.</TextBlock>

    <TextBlock DockPanel.Dock="Left"

                Background="Lavender">Some More Text</TextBlock>

    <DockPanel Background="Bisque">

      <StackPanel DockPanel.Dock="Top">

        <Button HorizontalAlignment="Left"

                Height="30px"

                Width="100px"

                Margin="10,10,10,10">Button1</Button>

        <Button HorizontalAlignment="Left"

                Height="30px"

                Width="100px"

                Margin="10,10,10,10">Button2</Button>

      </StackPanel>

      <TextBlock Background="LightGreen">Some Text Below the Buttons</TextBlock>

    </DockPanel>

  </DockPanel>

</Page>

 

这个例子的MyApp.xaml文件依然没有变化,但是Page1.xaml文件却增加了很多的内容。首先是布局(LayOut)的变化。[Page]元素里面的布局变为[DockPanel],而在[DockPanel]内部添加了三个[TextBlock]和一个嵌套的[DockPanel],在嵌套的[DockPanel]内部又嵌套了[StackPanel]。可见XAML的布局是可以任意的嵌套使用的。

 

VS2005中安装Orcas插件之后可以通过Design看到,控件的布局,如下图所示

 

 

下面先介绍一下[DockPanel][DockPanel]把整个布局分为4个部分,上(Top),下(Bottom),左(Left),右(Right),可以使用DockPanel.Dock="Left" 来设定控件或者子布局的位置。

 

例如:上例中的

<TextBlock Background="LightBlue" DockPanel.Dock="Top">Some Text</TextBlock>

指定Some Text文本位于[DockPanel]的上部;

 

<TextBlock DockPanel.Dock="Bottom" Background="LightYellow">Some text at the bottom of the page.</TextBlock>

 

指定Some text at the bottom of the page 文本位于[DockPanel]的下部;

 

<TextBlock DockPanel.Dock="Left" Background="Lavender">Some More Text</TextBlock>

 

指定Some More Text文本位于[DockPanel]的右部。

 

现在我们对[DockPanel]有了一些了解,那么我们的[DockPanel]的布局显示应该是下面的两个中的那种呢???

 

 

是第一张图所示的样子,原因在于我们的DockPanel.Dock="Top"定义在DockPanel.Dock="Left"的前面,如果想得到第二张图的现实,可以调整控件的声明顺序

 

    <TextBlock DockPanel.Dock="Left"

                Background="Lavender">Some More Text</TextBlock>

    <TextBlock Background="LightBlue"

                DockPanel.Dock="Top">Some Text</TextBlock>

    <TextBlock DockPanel.Dock="Bottom"

                Background="LightYellow">Some text at the bottom of the page.</TextBlock>

 

最后整个应用编译运行的结果如下图所示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值