相当于WPF WrapPanel的“FlowBreak”属性

Windows窗体中 ,将项目放置到FlowLayoutPanel控件中的作用与在Windows Presentation Foundation (WPF)中使用WrapPanel的方式非常相似。

FlowLayoutPanel中的项目可以将FlowBreak属性设置为true,以指示面板应该移动到项目之后的下一行的开头,而不pipe当前行中剩余多less空间。 基本上它是说“在此之后行分手”。

我现在正在使用WPF项目,我需要知道如何使用WPF WrapPanel控件完成等效。

我已经知道如何用蛮力来做到这一点。 我希望有一个更优雅和简单的方法。 有任何想法吗?

你可以使用这个技巧:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <WrapPanel x:Name="mywrappanel"> <TextBox Text="Text1"/> <TextBox Text="Text2"/> <TextBox Text="Text3"/> <Border Width="{Binding Path=ActualWidth, ElementName=mywrappanel}"/> <TextBox Text="Text4"/> <TextBox Text="Text5"/> </WrapPanel> </Page> 

它所做的是使用一个没有高度的虚拟元素(所以它是“隐藏的”),但宽度与WrapPanel相匹配,所以你可以确定它不能适合当前行,并填充下一个一。

您可以使用任何FrameworkElement派生元素作为虚拟…只是选择一个轻量级的,以避免不必要的内存使用。

你可以使用一个使用RelativeSource的绑定,如果你不想命名你的WrapPanel eg

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <WrapPanel x:Name="mywrappanel"> <TextBox Text="Text1"/> <TextBox Text="Text2"/> <TextBox Text="Text3"/> <Border Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WrapPanel}}}"/> <TextBox Text="Text4"/> <TextBox Text="Text5"/> </WrapPanel> </Page> 

为了更好地了解它在做什么…只要给它一个高度和一些颜色。

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <WrapPanel x:Name="mywrappanel"> <TextBox Text="Text1"/> <TextBox Text="Text2"/> <TextBox Text="Text3"/> <Border Height="20" Background="Red" Width="{Binding Path=ActualWidth, ElementName=mywrappanel}"/> <TextBox Text="Text4"/> <TextBox Text="Text5"/> </WrapPanel> </Page> 

在这里输入图像描述

如果你想让你的XAML更清洁一些(即没有绑定和虚拟Border ),那么你可以创建自己的FrameworkElement ,它的设计始终与祖先WrapPanel的宽度匹配。

请看这里的NewLine元素:

  • 将换行符/新行添加到WPF包装面板