[源码下载]

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

作者:webabcd

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

  • Panel
  • Canvas
  • RelativePanel
  • StackPanel
  • Grid

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

<Page
x:Class="Windows10.Controls.LayoutControl.PanelDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.LayoutControl"
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="10 0 10 10"> <!--
StackPanel - 流式布局控件,继承自 Panel,下面介绍 Panel 的相关知识点
Children - 子元素集合([ContentProperty(Name = "Children")])
Background - 背景色
ChildrenTransitions - 过渡效果集合
--> <StackPanel Margin="5" Background="Orange">
<StackPanel.Children>
<TextBlock Text="A Panel contains a collection of UIElement objects, which are in the Children property" Margin="5" />
<TextBlock Text="Panel elements do not receive focus by default" Margin="5" />
<TextBlock Text="To compel a panel element to receive focus, set the Focusable property to true" Margin="5" />
</StackPanel.Children>
<StackPanel.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition />
</TransitionCollection>
</StackPanel.ChildrenTransitions>
</StackPanel> </StackPanel>
</Grid>
</Page>

Controls/LayoutControl/PanelDemo.xaml.cs

/*
* Panel(基类) - 面板控件基类(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo.xaml)
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.LayoutControl
{
public sealed partial class PanelDemo : Page
{
public PanelDemo()
{
this.InitializeComponent();
}
}
}

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

<Page
x:Class="Windows10.Controls.LayoutControl.CanvasDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.LayoutControl"
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 的子元素使用绝对定位) Canvas.Left - 与上一层 Canvas 的 Y轴 间的距离,左上角为原点。仅在 Canvas 的子元素中设置有效
Canvas.Top - 与上一层 Canvas 的 X轴 间的距离,左上角为原点。仅在 Canvas 的子元素中设置有效
Canvas.ZIndex - 用于设置控件的 z-index 值(数值大的在前面)。 不要求必须在 Canvas 内 注:Canvas 不会因为自身的大小而限制子元素的大小
--> <!--
这里指定 Canvas.Left 和 Canvas.Top 是没用的,因为它的父亲不是 Canvas
-->
<Canvas Margin="10 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Orange" Width="100" Height="100"
Canvas.Left="100" Canvas.Top="100"> <Canvas Background="Blue" Width="200" Height="200" Canvas.Left="100" Canvas.Top="100">
<!--
Canvas 不会因为自身的大小而限制子元素的大小
-->
<TextBlock Text="TextBlock TextBlock TextBlock TextBlock TextBlock" /> <!--
Canvas.ZIndex 大的在前面,所以此处黑色会压着白色
-->
<StackPanel Background="Black" Width="50" Height="50" Canvas.Left="50" Canvas.Top="50" Canvas.ZIndex="10" />
<StackPanel Background="White" Width="50" Height="50" Canvas.Left="75" Canvas.Top="75" Canvas.ZIndex="1" />
</Canvas> </Canvas>
</Grid>
</Page>

Controls/LayoutControl/CanvasDemo.xaml.cs

/*
* Canvas - 绝对定位布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
* double GetLeft(UIElement element) - 获取指定 UIElement 的 Canvas.Left 值
* double GetTop(UIElement element) - 获取指定 UIElement 的 Canvas.Top 值
* int GetZIndex(UIElement element) - 获取指定 UIElement 的 Canvas.ZIndex 值
* SetLeft(UIElement element, double length) - 设置指定 UIElement 的 Canvas.Left 值
* SetTop(UIElement element, double length) - 设置指定 UIElement 的 Canvas.Top 值
* SetZIndex(UIElement element, int value) - 设置指定 UIElement 的 Canvas.ZIndex 值
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.LayoutControl
{
public sealed partial class CanvasDemo : Page
{
public CanvasDemo()
{
this.InitializeComponent();
}
}
}

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

<Page
x:Class="Windows10.Controls.LayoutControl.RelativePanelDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.LayoutControl"
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="10 0 10 10"> <!--
RelativePanel - 相对定位布局控件
BorderBrush - 边框画笔
BorderThickness - 边框宽度(左 上 右 下)
CornerRadius - 边框的角半径(左上 右上 右下 左下)
Padding - 内容与边框的间距(左 上 右 下) LeftOf, RightOf, Above, Below - 在指定元素的左边, 右边, 上边, 下边
AlignLeftWithPanel, AlignRightWithPanel, AlignTopWithPanel, AlignBottomWithPanel - 是否与 RelativePanel 容器的左边缘, 右边缘, 上边缘, 下边缘对齐
AlignLeftWith, AlignRightWith, AlignTopWith, AlignBottomWith - 与指定元素的左边缘, 右边缘, 上边缘, 下边缘对齐
AlignHorizontalCenterWith, AlignVerticalCenterWithPanel - 与指定元素水平居中对齐, 垂直居中对齐
AlignHorizontalCenterWithPanel, AlignVerticalCenterWithPanel - 是否相对于 RelativePanel 容器水平居中对齐, 垂直居中对齐 在 code-behind 中有对应的 Get... 和 Set... 方法来获取或设置上面这些附加属性
--> <RelativePanel Width="300" Margin="5"
HorizontalAlignment="Left" BorderBrush="Orange" BorderThickness="1" CornerRadius="0" Padding="0"> <Rectangle x:Name="rectangle1" Fill="Red" Height="50" Width="50"/> <Rectangle x:Name="rectangle2" Fill="Blue" Height="50" Width="50"
RelativePanel.RightOf="rectangle1" /> <Rectangle x:Name="rectangle3" Fill="Green" Height="50" Width="50"
RelativePanel.AlignRightWithPanel="True"/> <Rectangle x:Name="rectangle4" Fill="Yellow" Height="50" Width="50"
RelativePanel.Below="rectangle3" RelativePanel.AlignHorizontalCenterWith="rectangle3" /> <!--
rectangle5 的上边缘与 rectangle4 的上边缘对齐
-->
<Rectangle x:Name="rectangle5" Fill="Purple" Height="100" Width="100"
RelativePanel.AlignTopWith="rectangle4" RelativePanel.AlignHorizontalCenterWithPanel="True" /> </RelativePanel> </StackPanel>
</Grid>
</Page>

Controls/LayoutControl/RelativePanelDemo.xaml.cs

/*
* RelativePanel - 相对定位布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.LayoutControl
{
public sealed partial class RelativePanelDemo : Page
{
public RelativePanelDemo()
{
this.InitializeComponent();
}
}
}

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

<Page
x:Class="Windows10.Controls.LayoutControl.StackPanelDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.LayoutControl"
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="10 0 10 10"> <!--
StackPanel - 流式布局控件
Orientation - 控件内元素的排列方向
Horizontal - 水平排列
Vertical - 垂直排列
BorderBrush - 边框画笔
BorderThickness - 边框宽度(左 上 右 下)
CornerRadius - 边框的角半径(左上 右上 右下 左下)
Padding - 内容与边框的间距(左 上 右 下)
--> <StackPanel Background="Orange" Margin="5"
Orientation="Vertical" BorderBrush="Red" BorderThickness="1 2 3 4" CornerRadius="10 20 30 40" Padding="10 20 30 40">
<TextBlock Text="abc" Margin="5" />
<TextBlock Text="xyz" Margin="5" />
<TextBlock Text="123" Margin="5" />
</StackPanel> </StackPanel>
</Grid>
</Page>

Controls/LayoutControl/StackPanelDemo.xaml.cs

/*
* StackPanel - 流式布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.LayoutControl
{
public sealed partial class StackPanelDemo : Page
{
public StackPanelDemo()
{
this.InitializeComponent();
}
}
}

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

<Page
x:Class="Windows10.Controls.LayoutControl.GridDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.LayoutControl"
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 中的列
BorderBrush - 边框画笔
BorderThickness - 边框宽度(左 上 右 下)
CornerRadius - 边框的角半径(左上 右上 右下 左下)
Padding - 内容与边框的间距(左 上 右 下) RowDefinition - 行定义
Height - 高度
MinHeight - 最小高度
MaxHeight - 最大高度
ActualHeight - 获取真实高度 ColumnDefinition - 列定义
Width - 宽度
MinWidth - 最小宽度
MaxWidth - 最大宽度
ActualWidth - 获取真实宽度 Grid.Row - 控件所在的 Grid 的行的索引
code-behind: int GetRow(FrameworkElement element), SetRow(FrameworkElement element, int value)
Grid.Column - 控件所在的 Grid 的列的索引
code-behind: int GetColumn(FrameworkElement element), SetColumn(FrameworkElement element, int value)
Grid.RowSpan - 合并的行数。 控件所在行,以及控件所在行之后的需要连续合并的行的总行数
code-behind: int GetRowSpan(FrameworkElement element), SetRowSpan(FrameworkElement element, int value)
Grid.ColumnSpan - 合并的列数。 控件所在列,以及控件所在列之后的需要连续合并的列的总列数
code-behind: int GetColumnSpan(FrameworkElement element), SetColumnSpan(FrameworkElement element, int value) Width 和 Height 的可用值如下:
1、Auto - 自动设置为一个合适的值。默认值
2、Pixel - 像素值
3、* - 比例值。如 * 就代表全部,2* 和 7* 就代表后者是前者的 7/2 倍 注:Grid 的 HorizontalAlignment 属性和 VerticalAlignment 属性的默认值均是 Stretch
--> <Grid Background="Blue" Width="300" Height="300" BorderBrush="Orange" BorderThickness="10" CornerRadius="10" Canvas.ZIndex="10">
<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 值(不要求必须在 Canvas 内) 注:在 Grid 内的子元素的定位可以通过 Margin 来实现
-->
<Rectangle Margin="10 50 100 150" Fill="Green" Canvas.ZIndex="1" /> </Grid>
</Page>

Controls/LayoutControl/GridDemo.xaml.cs

/*
* Grid - 网格布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
*/ using Windows.UI.Xaml.Controls; namespace Windows10.Controls.LayoutControl
{
public sealed partial class GridDemo : Page
{
public GridDemo()
{
this.InitializeComponent();
}
}
}

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. SQLServer中处理每天四亿三千万记录

    我是如何在SQLServer中处理每天四亿三千万记录的   首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地 ...

  2. 通过如何通过js实现复制粘贴功能

    在ie中window.clipboardData(剪切板对象)是可以被获取,所以利用这个方法我们可以实现在IE当中复制粘贴的功能,demo如下! <html> <head> & ...

  3. Web学习-apache视图log刊物

    视图apache刊物 apache日志位置 不同的系统位置不同. widnows 假如是windows的话,xampp下应该是都存在的,直接去找apache的folder/log/access.log ...

  4. Image 对象

    <html> <body> <img id="compman" src="0387.jpg" alt="Computer ...

  5. TFS二次开发、C#知识点、SQL知识

    TFS二次开发.C#知识点.SQL知识总结目录   TFS二次开发系列 TFS二次开发系列:一.TFS体系结构和概念 TFS二次开发系列:二.TFS的安装 TFS二次开发系列:三.TFS二次开发的第一 ...

  6. C#利用Emit反射实现AOP,以及平台化框架封装思路

    C#利用Emit反射实现AOP,以及平台化框架封装思路 这是前两天扒的一段动态代理AOP代码,用的Emit反射生成子类来实现代理模式,在这里做个小笔记,然后讨论一下AOP框架的实现思路. 首先是主函数 ...

  7. 利用pdf2swf将PDF转换成SWF

    将PDF转换成SWF可以使用SWFTools工具中的pdf2swf(http://www.swftools.org/),CSDN快速免积分下载地址http://download.csdn.net/de ...

  8. Rustlang语言逐行处理文件的基本方法

    文件操作 需求: 将文件中的内容按行读取出来,然后对改行的数据进行处理,最后将处理后的行数据存放到新的文件中. 使用RUST来处理的方法如下. 首先引入需要的标准库: use std::io::pre ...

  9. 一个意想不到的Javascript内存泄漏

    原文:http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html 本周我在Meter的同事追踪到了一 ...

  10. appfog java jdbc mysql连接

    本来用得好好的openshift被墙了,无奈只能找过一个免费的空间.同学推荐appfog,appfog的确是一个很好用的空间,支持几乎所有主流的技术.我自己喜欢写点java,刚好我就开始使用了.app ...