等效于 WPF WrapPanel 的“FlowBreak"属性 [英] Equivalent to 'FlowBreak' property for WPF WrapPanel

查看:30
本文介绍了等效于 WPF WrapPanel 的“FlowBreak"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 窗体中,将项目放入FlowLayoutPanel 控件的工作方式与在Windows Presentation 中使用WrapPanel 的方式非常相似基础(WPF).

In Windows Forms, placing items into a FlowLayoutPanel control works very much the same way as using a WrapPanel in Windows Presentation Foundation (WPF).

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

Items in a FlowLayoutPanel can have the FlowBreak property set to true to indicate that the panel should move to the beginning of the next row after the item, regardless of how much space remains in the current row. Basically it's saying "line break after this".

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

I am now using WPF for a project and I need to know how I accomplish the equivalent when using a WPF WrapPanel control.

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

I already know how to do this with brute force. I'm hoping there is a more elegant and simple method. Any ideas?

推荐答案

你可以使用这个技巧:

<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 匹配,所以你可以确定它不能适合当前行,并填充"下一行.

What it is doing is using a dummy element with no height (so that it is "hidden") but with a width that matches the WrapPanel so you can be sure it can't fit on the current row, and "fills" the next one.

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

You can use any FrameworkElement derived element as the dummy...just choose a lightweight one to avoid unnecessary memory usage.

如果您不想命名您的 WrapPanel,例如

You can use a binding that uses RelativeSource if you don't want to have to name your WrapPanel e.g.

<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>

为了更好地可视化它在做什么...只需给它一个高度和一些颜色.

To better visualize what it's doing...just give it a height and some colour.

<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 的宽度.

If you want your XAML to be a bit cleaner/clearer (i.e. not have the binding, and dummy Border), then you could create your own FrameworkElement that's designed to always match the width of an ancestor WrapPanel.

在此处查看 NewLine 元素:

这篇关于等效于 WPF WrapPanel 的“FlowBreak"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆