WPF实例学习(A-03)
2006-07-02 19:13
573 查看
代码所在位置: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>
最后整个应用编译运行的结果如下图所示:
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>
最后整个应用编译运行的结果如下图所示:
相关文章推荐
- WPF实例学习(A-03)
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- Django学习03---urls.py路由用法与实例
- 【Spring学习03】Spring简单入门实例
- vue学习03----vue实例以及生命周期
- WPF实例学习(A-02)
- WPF实例学习(A-04)
- WPF学习笔记(03) - 华丽的HelloWorld
- WPF学习实例 -- 股票行情查看程序
- 重学Python - Day 03 - python基础 -> while循环实例 + Continue && break的应用 + 列表的初步学习
- WPF实例学习(00)
- WPF实例学习(A-04)
- WPF学习笔记(03) - 华丽的HelloWorld
- WPF 学习记录——源码实例
- WPF实例学习(A-01)
- WPF实例学习(A-05)
- WPF学习03:Element Binding
- [转载] WPF实例学习 (A-01)
- WPF实例学习(A-02)