[源码下载]

背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid

作者:webabcd

介绍
背水一战 Windows 10 之 控件(布局类)

  • Panel
  • Canvas
  • RelativePanel
  • StackPanel
  • Grid

示例
1、Panel(基类) 的示例
Controls/LayoutControl/PanelDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.LayoutControl.PanelDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.LayoutControl"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <!--
  14. StackPanel - 流式布局控件,继承自 Panel,下面介绍 Panel 的相关知识点
  15. Children - 子元素集合([ContentProperty(Name = "Children")])
  16. Background - 背景色
  17. ChildrenTransitions - 过渡效果集合
  18. -->
  19.  
  20. <StackPanel Margin="5" Background="Orange">
  21. <StackPanel.Children>
  22. <TextBlock Text="A Panel contains a collection of UIElement objects, which are in the Children property" Margin="5" />
  23. <TextBlock Text="Panel elements do not receive focus by default" Margin="5" />
  24. <TextBlock Text="To compel a panel element to receive focus, set the Focusable property to true" Margin="5" />
  25. </StackPanel.Children>
  26. <StackPanel.ChildrenTransitions>
  27. <TransitionCollection>
  28. <EntranceThemeTransition />
  29. </TransitionCollection>
  30. </StackPanel.ChildrenTransitions>
  31. </StackPanel>
  32.  
  33. </StackPanel>
  34. </Grid>
  35. </Page>

Controls/LayoutControl/PanelDemo.xaml.cs

  1. /*
  2. * Panel(基类) - 面板控件基类(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo.xaml)
  3. */
  4.  
  5. using Windows.UI.Xaml.Controls;
  6.  
  7. namespace Windows10.Controls.LayoutControl
  8. {
  9. public sealed partial class PanelDemo : Page
  10. {
  11. public PanelDemo()
  12. {
  13. this.InitializeComponent();
  14. }
  15. }
  16. }

2、Canvas 的示例
Controls/LayoutControl/CanvasDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.LayoutControl.CanvasDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.LayoutControl"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <!--
  13. Canvas - 绝对定位布局控件(Canvas 的子元素使用绝对定位)
  14.  
  15. Canvas.Left - 与上一层 Canvas 的 Y轴 间的距离,左上角为原点。仅在 Canvas 的子元素中设置有效
  16. Canvas.Top - 与上一层 Canvas 的 X轴 间的距离,左上角为原点。仅在 Canvas 的子元素中设置有效
  17. Canvas.ZIndex - 用于设置控件的 z-index 值(数值大的在前面)。 不要求必须在 Canvas 内
  18.  
  19. 注:Canvas 不会因为自身的大小而限制子元素的大小
  20. -->
  21.  
  22. <!--
  23. 这里指定 Canvas.Left 和 Canvas.Top 是没用的,因为它的父亲不是 Canvas
  24. -->
  25. <Canvas Margin="10 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Orange" Width="100" Height="100"
  26. Canvas.Left="100" Canvas.Top="100">
  27.  
  28. <Canvas Background="Blue" Width="200" Height="200" Canvas.Left="100" Canvas.Top="100">
  29. <!--
  30. Canvas 不会因为自身的大小而限制子元素的大小
  31. -->
  32. <TextBlock Text="TextBlock TextBlock TextBlock TextBlock TextBlock" />
  33.  
  34. <!--
  35. Canvas.ZIndex 大的在前面,所以此处黑色会压着白色
  36. -->
  37. <StackPanel Background="Black" Width="50" Height="50" Canvas.Left="50" Canvas.Top="50" Canvas.ZIndex="10" />
  38. <StackPanel Background="White" Width="50" Height="50" Canvas.Left="75" Canvas.Top="75" Canvas.ZIndex="1" />
  39. </Canvas>
  40.  
  41. </Canvas>
  42. </Grid>
  43. </Page>

Controls/LayoutControl/CanvasDemo.xaml.cs

  1. /*
  2. * Canvas - 绝对定位布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
  3. * double GetLeft(UIElement element) - 获取指定 UIElement 的 Canvas.Left 值
  4. * double GetTop(UIElement element) - 获取指定 UIElement 的 Canvas.Top 值
  5. * int GetZIndex(UIElement element) - 获取指定 UIElement 的 Canvas.ZIndex 值
  6. * SetLeft(UIElement element, double length) - 设置指定 UIElement 的 Canvas.Left 值
  7. * SetTop(UIElement element, double length) - 设置指定 UIElement 的 Canvas.Top 值
  8. * SetZIndex(UIElement element, int value) - 设置指定 UIElement 的 Canvas.ZIndex 值
  9. */
  10.  
  11. using Windows.UI.Xaml.Controls;
  12.  
  13. namespace Windows10.Controls.LayoutControl
  14. {
  15. public sealed partial class CanvasDemo : Page
  16. {
  17. public CanvasDemo()
  18. {
  19. this.InitializeComponent();
  20. }
  21. }
  22. }

3、RelativePanel 的示例
Controls/LayoutControl/RelativePanelDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.LayoutControl.RelativePanelDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.LayoutControl"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <!--
  14. RelativePanel - 相对定位布局控件
  15. BorderBrush - 边框画笔
  16. BorderThickness - 边框宽度(左 上 右 下)
  17. CornerRadius - 边框的角半径(左上 右上 右下 左下)
  18. Padding - 内容与边框的间距(左 上 右 下)
  19.  
  20. LeftOf, RightOf, Above, Below - 在指定元素的左边, 右边, 上边, 下边
  21. AlignLeftWithPanel, AlignRightWithPanel, AlignTopWithPanel, AlignBottomWithPanel - 是否与 RelativePanel 容器的左边缘, 右边缘, 上边缘, 下边缘对齐
  22. AlignLeftWith, AlignRightWith, AlignTopWith, AlignBottomWith - 与指定元素的左边缘, 右边缘, 上边缘, 下边缘对齐
  23. AlignHorizontalCenterWith, AlignVerticalCenterWithPanel - 与指定元素水平居中对齐, 垂直居中对齐
  24. AlignHorizontalCenterWithPanel, AlignVerticalCenterWithPanel - 是否相对于 RelativePanel 容器水平居中对齐, 垂直居中对齐
  25.  
  26. 在 code-behind 中有对应的 Get... 和 Set... 方法来获取或设置上面这些附加属性
  27. -->
  28.  
  29. <RelativePanel Width="300" Margin="5"
  30. HorizontalAlignment="Left" BorderBrush="Orange" BorderThickness="1" CornerRadius="0" Padding="0">
  31.  
  32. <Rectangle x:Name="rectangle1" Fill="Red" Height="50" Width="50"/>
  33.  
  34. <Rectangle x:Name="rectangle2" Fill="Blue" Height="50" Width="50"
  35. RelativePanel.RightOf="rectangle1" />
  36.  
  37. <Rectangle x:Name="rectangle3" Fill="Green" Height="50" Width="50"
  38. RelativePanel.AlignRightWithPanel="True"/>
  39.  
  40. <Rectangle x:Name="rectangle4" Fill="Yellow" Height="50" Width="50"
  41. RelativePanel.Below="rectangle3" RelativePanel.AlignHorizontalCenterWith="rectangle3" />
  42.  
  43. <!--
  44. rectangle5 的上边缘与 rectangle4 的上边缘对齐
  45. -->
  46. <Rectangle x:Name="rectangle5" Fill="Purple" Height="100" Width="100"
  47. RelativePanel.AlignTopWith="rectangle4" RelativePanel.AlignHorizontalCenterWithPanel="True" />
  48.  
  49. </RelativePanel>
  50.  
  51. </StackPanel>
  52. </Grid>
  53. </Page>

Controls/LayoutControl/RelativePanelDemo.xaml.cs

  1. /*
  2. * RelativePanel - 相对定位布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
  3. */
  4.  
  5. using Windows.UI.Xaml.Controls;
  6.  
  7. namespace Windows10.Controls.LayoutControl
  8. {
  9. public sealed partial class RelativePanelDemo : Page
  10. {
  11. public RelativePanelDemo()
  12. {
  13. this.InitializeComponent();
  14. }
  15. }
  16. }

4、StackPanel 的示例
Controls/LayoutControl/StackPanelDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.LayoutControl.StackPanelDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.LayoutControl"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel HorizontalAlignment="Left" Margin="10 0 10 10">
  12.  
  13. <!--
  14. StackPanel - 流式布局控件
  15. Orientation - 控件内元素的排列方向
  16. Horizontal - 水平排列
  17. Vertical - 垂直排列
  18. BorderBrush - 边框画笔
  19. BorderThickness - 边框宽度(左 上 右 下)
  20. CornerRadius - 边框的角半径(左上 右上 右下 左下)
  21. Padding - 内容与边框的间距(左 上 右 下)
  22. -->
  23.  
  24. <StackPanel Background="Orange" Margin="5"
  25. Orientation="Vertical" BorderBrush="Red" BorderThickness="1 2 3 4" CornerRadius="10 20 30 40" Padding="10 20 30 40">
  26. <TextBlock Text="abc" Margin="5" />
  27. <TextBlock Text="xyz" Margin="5" />
  28. <TextBlock Text="123" Margin="5" />
  29. </StackPanel>
  30.  
  31. </StackPanel>
  32. </Grid>
  33. </Page>

Controls/LayoutControl/StackPanelDemo.xaml.cs

  1. /*
  2. * StackPanel - 流式布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
  3. */
  4.  
  5. using Windows.UI.Xaml.Controls;
  6.  
  7. namespace Windows10.Controls.LayoutControl
  8. {
  9. public sealed partial class StackPanelDemo : Page
  10. {
  11. public StackPanelDemo()
  12. {
  13. this.InitializeComponent();
  14. }
  15. }
  16. }

5、Grid 的示例
Controls/LayoutControl/GridDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.LayoutControl.GridDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.LayoutControl"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <!--
  13. Grid - 网格布局控件
  14. Grid.RowDefinitions - 用于定义 Grid 中的行
  15. Grid.ColumnDefinitions - 用于定义 Grid 中的列
  16. BorderBrush - 边框画笔
  17. BorderThickness - 边框宽度(左 上 右 下)
  18. CornerRadius - 边框的角半径(左上 右上 右下 左下)
  19. Padding - 内容与边框的间距(左 上 右 下)
  20.  
  21. RowDefinition - 行定义
  22. Height - 高度
  23. MinHeight - 最小高度
  24. MaxHeight - 最大高度
  25. ActualHeight - 获取真实高度
  26.  
  27. ColumnDefinition - 列定义
  28. Width - 宽度
  29. MinWidth - 最小宽度
  30. MaxWidth - 最大宽度
  31. ActualWidth - 获取真实宽度
  32.  
  33. Grid.Row - 控件所在的 Grid 的行的索引
  34. code-behind: int GetRow(FrameworkElement element), SetRow(FrameworkElement element, int value)
  35. Grid.Column - 控件所在的 Grid 的列的索引
  36. code-behind: int GetColumn(FrameworkElement element), SetColumn(FrameworkElement element, int value)
  37. Grid.RowSpan - 合并的行数。 控件所在行,以及控件所在行之后的需要连续合并的行的总行数
  38. code-behind: int GetRowSpan(FrameworkElement element), SetRowSpan(FrameworkElement element, int value)
  39. Grid.ColumnSpan - 合并的列数。 控件所在列,以及控件所在列之后的需要连续合并的列的总列数
  40. code-behind: int GetColumnSpan(FrameworkElement element), SetColumnSpan(FrameworkElement element, int value)
  41.  
  42. Width 和 Height 的可用值如下:
  43. 1、Auto - 自动设置为一个合适的值。默认值
  44. 2、Pixel - 像素值
  45. 3、* - 比例值。如 * 就代表全部,2* 和 7* 就代表后者是前者的 7/2 倍
  46.  
  47. 注:Grid 的 HorizontalAlignment 属性和 VerticalAlignment 属性的默认值均是 Stretch
  48. -->
  49.  
  50. <Grid Background="Blue" Width="300" Height="300" BorderBrush="Orange" BorderThickness="10" CornerRadius="10" Canvas.ZIndex="10">
  51. <Grid.RowDefinitions>
  52. <RowDefinition Height="50" />
  53. <RowDefinition Height="3*" />
  54. <RowDefinition Height="7*" />
  55. <RowDefinition Height="*" MinHeight="50" MaxHeight="100" />
  56. <RowDefinition Height="Auto" />
  57. </Grid.RowDefinitions>
  58. <Grid.ColumnDefinitions>
  59. <ColumnDefinition />
  60. <ColumnDefinition />
  61. <ColumnDefinition />
  62. </Grid.ColumnDefinitions>
  63.  
  64. <TextBox Grid.Row="0" Grid.Column="0" Background="red" Text="webabcd" />
  65. <TextBox Grid.Row="0" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
  66. <TextBox Grid.Row="1" Grid.Column="0" Background="red" Text="webabcd" />
  67. <TextBox Grid.Row="1" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
  68. <TextBox Grid.Row="2" Grid.Column="0" Background="red" Text="webabcd" />
  69. <TextBox Grid.Row="2" Grid.Column="1" Background="red" Text="webabcd" Grid.RowSpan="2" VerticalAlignment="Bottom" />
  70. <TextBox Grid.Row="2" Grid.Column="2" Background="red" Text="webabcd" />
  71. <TextBox Grid.Row="3" Grid.Column="2" Background="red" Text="webabcd" />
  72. <TextBox Grid.Row="4" Grid.Column="2" Background="red" Text="webabcd" />
  73. </Grid>
  74.  
  75. <!--
  76. Canvas.ZIndex - 用于设置控件的 z-index 值(不要求必须在 Canvas 内)
  77.  
  78. 注:在 Grid 内的子元素的定位可以通过 Margin 来实现
  79. -->
  80. <Rectangle Margin="10 50 100 150" Fill="Green" Canvas.ZIndex="1" />
  81.  
  82. </Grid>
  83. </Page>

Controls/LayoutControl/GridDemo.xaml.cs

  1. /*
  2. * Grid - 网格布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
  3. */
  4.  
  5. using Windows.UI.Xaml.Controls;
  6.  
  7. namespace Windows10.Controls.LayoutControl
  8. {
  9. public sealed partial class GridDemo : Page
  10. {
  11. public GridDemo()
  12. {
  13. this.InitializeComponent();
  14. }
  15. }
  16. }

OK
[源码下载]

背水一战 Windows 10 (38) - 控件(布局类): Panel, Canvas, RelativePanel, StackPanel, Grid的更多相关文章

  1. 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView

    [源码下载] 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView 作者:webabcd ...

  2. 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性

    [源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...

  3. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  4. 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment

    [源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...

  5. 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid

    [源码下载] 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingS ...

  6. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  7. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  8. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  9. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

随机推荐

  1. Asterisk 未来之路3.0_0001

    原文:Asterisk 未来之路3.0_0001 第一章:电信技术革命 刚开始他们忽视你,然后他们嘲笑你,然后他们向你挑战,最后你赢了 ---Mahatma Ganhdi 在5年前,我最初规划写一本关 ...

  2. Asp.Net MVC页面静态化功能实现一:利用IHttpModule,摒弃ResultFilter

    上一篇有提到利用IHttpModule和ResultFilter实现页面静态化功能.后来经过一些改动,将ResultFilter中要实现的功能全部转移到IHttpModule中来实现 Asp.Net ...

  3. 简单分析android textview xml 的属性设置

    android:ems 设置TextView的宽度为N个字符的宽度. 这样的好处就是,在定义编辑框空间输入多少字符的时候,可以根据固定的值设置编辑框宽度.保证边框和文字的宽度统一.android:ma ...

  4. 手机新闻网站,掌上移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发

    我们坐在地铁,经常来查看新浪手机新闻,腾讯新闻.或者刷微信看新闻更多功能.你有没有想过如何实现这些目标.移动互联网,更活泼. 因为HTML5到,jQuery Moblie到.今天我用jqm为了给你一个 ...

  5. 64位平台支持大于2 GB大小的数组

    64位平台支持大于2 GB大小的数组 64位平台.NET Framework数组限制不能超过2GB大小.这种限制对于需要使用到大型矩阵和向量计算的工作人员来说,是一个非常大问题. 无论RAM容量有多大 ...

  6. Varnish缓存服务

    Varnish缓存服务详解及应用实现   1.varnish的基本介绍   Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已 ...

  7. 云优化的概念、Entity Framework 7.0、简单吞吐量压力测试

    云优化的概念.Entity Framework 7.0.简单吞吐量压力测试 继续上一篇<开发 ASP.NET vNext 初步总结(使用Visual Studio 2014 CTP1)>之 ...

  8. 什么是gulp

    gulp:入门简介   本文是gulp的入门级介绍,主要内容包括什么是gulp,gulp与grunt有什么区别,gulp可以解决grunt存在的哪些问题,以及一个简单的说明例子. 什么是gulp gu ...

  9. 字符串匹配算法 之 基于DFA(确定性有限自动机)

    确定有限自动机定义:http://en.wikipedia.org/wiki/Deterministic_finite_automaton 自动机在字符串匹配中的应用 #include<stdi ...

  10. .NET中文乱码解决方案

    前言:最近升级一个由VS05开发的项目,当迁移至VS10后,试运行,啊~!我文盲了,怎么一个汉字都不认识了!(乱码纷纷的说) 说明:本文以将项目改为UTF8编码为例. 解决之道 1.修改配置文件 &l ...