原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

[源码下载]

重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之布局控件

  • Canvas - 绝对定位式布局
  • Grid - 网格式布局
  • StackPanel - 流式布局
  • VirtualizingStackPanel - 仅能用于 ItemsControl
  • WrapGrid - 仅能用于 ItemsControl
  • VariableSizedWrapGrid - 用于 Wrap 子元素集合

示例
1、Canvas 的 Demo
CanvasDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.CanvasDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<!--
Canvas - 绝对定位式布局
Canvas.Left - 与上一层 Canvas 的 Y轴 间的距离,左上角为原点
Canvas.Top - 与上一层 Canvas 的 X轴 间的距离,左上角为原点
Canvas.ZIndex - 用于设置任意控件的 z-index 值 注:Canvas 基于坐标定位,其不会因为自身的大小而限制子元素的大小
-->
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Background="Red" Width="100" Height="100" Margin="120 0 0 0"> <Canvas Background="Green" Width="200" Height="200" Canvas.Left="120" Canvas.Top="120" >
<TextBlock Text="TextBlock" Canvas.Top="20" />
</Canvas> </Canvas>
</Grid>
</Page>

2、Grid 的 Demo
GridDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.GridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<!--
Grid - 网格式布局
Grid.RowDefinitions - 用于定义 Grid 中的行
Grid.ColumnDefinitions - 用于定义 Grid 中的列
Width - 宽度
MinWidth - 最小宽度
MaxWidth - 最大宽度
Height - 高度
MinHeight - 最小高度
MaxHeight - 最大高度
Grid.Row - 控件所在的 Grid 的行的索引
Grid.Column - 控件所在的 Grid 的列的索引
Grid.RowSpan - 合并行。 控件所在行,以及控件所在行之后的需要连续合并的行的总行数
Grid.ColumnSpan - 合并列。 控件所在列,以及控件所在列之后的需要连续合并的列的总列数
Canvas.ZIndex - 用于设置任意控件的 z-index 值 Width 和 Height 的可用值如下:
1、Auto - 自动设置为一个合适的值。默认值
2、Pixel - 像素值
3、* - 比例值。如 * 就是全部,2* 和 8* 就是分别占 20% 和 80%
-->
<Grid Background="Blue" Width="300" Height="300" Canvas.ZIndex="100">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="3*" />
<RowDefinition Height="7*" />
<RowDefinition Height="*" MinHeight="50" MaxHeight="100" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions> <TextBox Grid.Row="0" Grid.Column="0" Background="red" Text="webabcd" />
<TextBox Grid.Row="0" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
<TextBox Grid.Row="1" Grid.Column="0" Background="red" Text="webabcd" />
<TextBox Grid.Row="1" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
<TextBox Grid.Row="2" Grid.Column="0" Background="red" Text="webabcd" />
<TextBox Grid.Row="2" Grid.Column="1" Background="red" Text="webabcd" Grid.RowSpan="2" VerticalAlignment="Bottom" />
<TextBox Grid.Row="2" Grid.Column="2" Background="red" Text="webabcd" />
<TextBox Grid.Row="3" Grid.Column="2" Background="red" Text="webabcd" />
<TextBox Grid.Row="4" Grid.Column="2" Background="red" Text="webabcd" />
</Grid> <!--
Canvas.ZIndex - 用于设置任意控件的 z-index 值 说明:
1、Grid 的 HorizontalAlignment 属性和 VerticalAlignment 属性的默认值均是 Stretch
2、在 Grid 内的所有子元素均需要通过 Margin 属性进行相对定位
-->
<Rectangle Margin="10 50 100 150" Fill="Green" Canvas.ZIndex="10" /> </Grid>
</Page>

3、StackPanel 的 Demo
StackPanelDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.StackPanelDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel HorizontalAlignment="Left" Margin="120 0 0 0"> <!--
StackPanel - 流式布局
Orientation - StackPanel 控件内对象的排列方向
Horizontal - 水平排列
Vertical - 垂直排列
-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="a" Margin="5" />
<TextBlock Text="b" Margin="5" />
<TextBlock Text="c" Margin="5" />
</StackPanel> <StackPanel Orientation="Vertical">
<TextBlock Text="a" Margin="5" />
<TextBlock Text="b" Margin="5" />
<TextBlock Text="c" Margin="5" />
</StackPanel> </StackPanel>
</Grid>
</Page>

4、VirtualizingStackPanel 的 Demo
VirtualizingStackPanelDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.VirtualizingStackPanelDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667">
<Run>仅能用于 ItemsControl</Run>
<LineBreak />
<Run>请参见 Controls/ListBoxDemo.xaml</Run>
</TextBlock> </StackPanel>
</Grid>
</Page>

5、WrapGrid 的 Demo
WrapGridDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.WrapGridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667">
<Run>仅能用于 ItemsControl</Run>
<LineBreak />
<Run>请参见 Controls/GridView/Demo.xaml</Run>
</TextBlock> </StackPanel>
</Grid>
</Page>

6、VariableSizedWrapGrid 的 Demo
VariableSizedWrapGridDemo.xaml

<Page
x:Class="XamlDemo.Controls.Layout.VariableSizedWrapGridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667">
<Run>另请参见 Controls/GridView/VariableSized.xaml</Run>
</TextBlock> <!--
VariableSizedWrapGrid
1、用于 Wrap 子元素集合
2、关于 VariableSized 的功能详见 Controls/GridView/VariableSized.xaml
-->
<VariableSizedWrapGrid Orientation="Horizontal" HorizontalAlignment="Left" Background="Green" Width="1000" Margin="0 10 0 0">
<VariableSizedWrapGrid.Children>
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
<Image Source="/Assets/Logo.png" Width="100" Height="100" Margin="10" />
</VariableSizedWrapGrid.Children>
</VariableSizedWrapGrid> </StackPanel>
</Grid>
</Page>

OK
[源码下载]

重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  3. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  4. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  5. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  6. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  7. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  8. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  9. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

随机推荐

  1. hdu 2842 Chinese Rings

    点击打开hdu2842 思路: 矩阵快速幂 分析: 1 题目的意思是给定n个环,和一些规则要把所有的环全部拆下最少需要的步数 2 题目规定如果要拆第n个环,那么第n-1个要挂着,n-2环要被拆下.那么 ...

  2. vim经常使用命令总结

    vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是非常主要的东西,也常常被用到,总结例如以下: v    从光标当前位置開始,光标所经过的地方会被选中,再按一下v结束. V     ...

  3. 什么是Java “实例化”

    实例化:对象也是引用数据类型,只能使用new运算符从堆中分配内存: 使用已经定义好的类,创建该类对象的过程称为“实例化”. 只有先实例化类的对象,才可以访问到类中的成员(属性和方法). 使用成员运算符 ...

  4. R语言与数据分析之九:时间内序列--HoltWinters指数平滑法

    今天继续就指数平滑法中最复杂的一种时间序列:有增长或者减少趋势而且存在季节性波动的时间序列的预測算法即Holt-Winters和大家分享.这样的序列能够被分解为水平趋势部分.季节波动部分,因此这两个因 ...

  5. hdu1054(最小顶点覆盖)

    传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...

  6. aix 下 实现goldengate 随os启动而自己主动启动的脚本

    aix 下 实现goldengate 随os启动而自己主动启动的脚本: 1.用oracle用户建立/u01/info.txt,文件内容例如以下: sh date start mgr 2.chmod + ...

  7. TMS320F28335项目开发记录5_28335之CCS编程基础

    CCS开发环境已经为我们封装好了很多片内外设寄存器的结构体,我们仅仅须要包括对应的官方的头文件就能够使用了,那么它的内部详细是怎样实现的呢? 以下来一个典型的样例: 1.使用结构体和联合体 A.用st ...

  8. 如何做程序猿SOHO它定购家庭赚外快?

    做为一名程序猿.我想大多数人除了平时削尖了脑袋研究各种各样的技术之外. ArticleId=28404183" width="1" height="1" ...

  9. 重构后的ConditionHelper

    两三个月前曾写过<重构ConditionHelper>的随笔,但不知是因为写得不够好还是没有什么新意,我发表至博客园首页时被屏蔽了,本着好的知识应该分享给更多人,加之新项目已交付用户使用所 ...

  10. 浅谈JAVA ThreadPoolExecutor(转)

    这篇文章分为两部分,前面是ThreadPoolExecutor的一些基本知识,后一部分则是Mina中一个特殊的ThreadPoolExecutor代码解析.算是我的Java学习笔记吧. 基础 在我看来 ...