【问题标题】:Equivalent to 'FlowBreak' property for WPF WrapPanel等效于 WPF WrapPanel 的“FlowBreak”属性
【发布时间】:2015-04-02 01:35:01
【问题描述】:

Windows 窗体 中,将项目放入 FlowLayoutPanel 控件的工作方式与在 Windows 演示文稿中使用 WrapPanel 的方式非常相似基金会 (WPF)。

FlowLayoutPanel 中的项目可以将 FlowBreak 属性设置为 true 以指示面板应该移动到项目之后的下一行的开头,无论如何当前行中还有很多空间。基本上是说“在此之后换行”。

我现在在一个项目中使用 WPF,我需要知道在使用 WPF WrapPanel 控件时如何完成等效操作。

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

【问题讨论】:

  • 那么你如何使用蛮力呢?如果你不解释,有人可以给你一个同样使用蛮力的答案。
  • “蛮力”是指使用一系列嵌套的 WrapPanel 和/或 StackPanel。

标签: wpf windows winforms


【解决方案1】:

你可以使用这个技巧:

<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 派生元素作为虚拟元素...只需选择一个轻量级元素以避免不必要的内存使用。

如果您不想命名 WrapPanel,则可以使用使用 RelativeSource 的绑定,例如

<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,它旨在始终匹配祖先 @987654332 的宽度@。

在此处查看NewLine 元素:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多