WPF 入门教程WrapPanel介绍

92 篇文章 9 订阅
92 篇文章 23 订阅

WrapPanel将定位每个子控件的旁边,另外,水平方向(默认值)或垂直,直到没有更多的空间,在那里将换到下一行,然后继续。当您想要一个垂直或水平列表控件在没有更多空间时自动换行时使用它。

当 WrapPanel 使用 Horizo​​ntal 方向时,子控件将

被赋予相同的高度,基于最高的项目。当 WrapPanel 为垂直方向时,子控件将被赋予相同的宽度,基于最宽的项目。

在第一个示例中,我们将检查具有默认(水平)方向的 WrapPanel:

<Window x:Class="WpfTutorialSamples.Panels.WrapPanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WrapPanel" Height="300" Width="300">
	<WrapPanel>
		<Button>Test button 1</Button>
		<Button>Test button 2</Button>
		<Button>Test button 3</Button>
		<Button Height="40">Test button 4</Button>
		<Button>Test button 5</Button>
		<Button>Test button 6</Button>
	</WrapPanel>
</Window>

请注意我如何在第二行中的一个按钮上设置特定高度。在生成的屏幕截图中,您将看到这会导致整行按钮具有相同的高度,而不是所需的高度,如第一行所示。您还会注意到面板的功能正如其名称所暗示的那样:当内容无法容纳更多内容时,它会包装内容。在这种情况下,第四个按钮无法放入第一行,因此它会自动换行到下一行。

如果您将窗口缩小,从而使可用空间变小,您将看到面板如何立即适应它:

当您将方向设置为垂直时,所有这些行为也是如此。这是与之前完全相同的示例,但使用 Vertical WrapPanel:

<Window x:Class="WpfTutorialSamples.Panels.WrapPanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WrapPanel" Height="120" Width="300">
	<WrapPanel Orientation="Vertical">
		<Button>Test button 1</Button>
		<Button>Test button 2</Button>
		<Button>Test button 3</Button>
		<Button Width="140">Test button 4</Button>
		<Button>Test button 5</Button>
		<Button>Test button 6</Button>
	</WrapPanel>
</Window>

您可以看到按钮是如何垂直而不是水平的,因为它们到达窗口底部而环绕。在本例中,我为第四个按钮设置了更宽的宽度,您将看到同一列中的按钮也具有相同的宽度,就像我们在水平示例中看到的按钮高度一样。

请注意,虽然 Horizo​​ntal WrapPanel 将匹配同一行中的高度,而 Vertical WrapPanel 将匹配同一列中的宽度,但 Vertical WrapPanel 中的高度不匹配,Horizo​​ntal WrapPanel 中的宽度不匹配。看看这个例子,它是 Vertical WrapPanel 但第四个按钮获得自定义宽度和高度:

<Button Width="140" Height="44">Test button 4</Button>

它看起来像这样:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值