WPF 控件专题 StackPanel控件详解

1、StackPanel 介绍

    将子元素排列成水平或垂直的一行,属于布局控件,通俗说叫栈式面板。可将包含的元素在水平或垂直方向排成一条线,当移除一个元素后,后面的元素会自动向前填充空缺。

    StackPanel 中的子元素超出的部分,被截取被隐藏;StackPanel 自身可以嵌套使用,嵌套在其他布局控件中,和其他布局控件结合使用。

**************************************************************************************************************

2、StackPanel 的几个常用的属性介绍

    Background:Brush对象,背景。

    FlowDirection:获取或设置文本和其他用户界面 (UI) 元素在控制其布局的任何父元素内流动的方向,默认是从左往右显示的;RightToLeft:从右到左;LeftToRight:从左到右。

    Height/Width:高度/宽度;MaxHeight/MaxWidth:最大高度/最大宽度;MinHeight/MinWidth:最小高度/最小宽度;

    HorizontalAlignment/VerticalAlignment:设置内部元素水平对齐/垂直对齐;

    IsEnabled:使能,是否可用;

    IsHitTestVisible:声明是否可以返回此元素作为其呈现内容的某些部分的点击测试结果;

    IsVisible/Visibility:此元素在界面中是否可见;

    Margin:获取或设置元素的外边距;顺序为:上下左右;

    Name:获取和设置此元素的标识名称;

    Opacity:透明度;

    Orientation:获取或设置一个值,该值指示子元素的堆叠维度。Horizontal:水平排布;Vertical:垂直排布;

**************************************************************************************************************

3、具体示例,这里使用Image作为StackPanel的子元素进行布局显示;

<Grid>
	<TextBlock Text="StackPanel 水平方向显示" Foreground="#dddddd" FontSize="15" HorizontalAlignment="Center"/>
	<!--水平显示-->
	<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 15">
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
	</StackPanel>

	<GridSplitter Height="1" Background="Cyan" Margin="0 130 0 0" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>

	<TextBlock Text="StackPanel 垂直方向显示" Foreground="#dddddd" FontSize="15" HorizontalAlignment="Center" Margin="0 135 0 0"/>
	<!--垂直显示-->
	<StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="0 150 0 0">
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
	</StackPanel>
</Grid>

设置FlowDirection 属性,从右往左显示示例代码如下

<Grid>
	<!--水平显示-->
	<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 15" FlowDirection="RightToLeft">
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
		<Image Width="150" Height="100" Margin="10" Source="./huahua.png" Stretch="Fill"/>
	</StackPanel>
</Grid>

**************************************************************************************************************

4、效果图

    设置FlowDirection 属性,从右往左显示效果图

    从上面代码和效果图可以看出,水平显示时,每个子元素控件都是独占一列的;垂直显示时,每一个子元素控件都是独占一行的。

 **************************************************************************************************************

5、总结和扩展

    StackPanel 是一个受欢迎的面板,由于它方便好用,它会顺序对它的子元素进行排列。它是少数几个没有定义任何附加属性的面板之一。因为没有附加属性来排列子元素,只有一种方法能够定制StackPanel的行为—设置Orientation属性为Horizontal或Vertical,Vertical是默认值。

    StackPanel 默认是垂直居中、水平居右显示的,可以通过设置HorizontalAlignment和VerticalAlignment属性进行布局更改;

    注:当把StackPanel的FlowDirection属性设置为RightToLeft,Orientation属性设置为Horizontal,StackPanel将从右向左排列元素,否则不起作用。

**************************************************************************************************************

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值