将换行符/换行添加到WPF换行面板

Dom*_*c K 13 wpf wrappanel

有谁知道甚至可以在WPF Wrap面板中输入换行符?它违背了包装面板的用途,所以我不确定它是否可行.

如果不是,是否有任何其他WPF控件实际上允许我输入换行符并支持添加childern(我自己的自定义控件?)

Jay*_*Jay 15

这是一个换行符WrapPanel:

<WrapPanel>
    <TextBlock Text="&#xD;"/>
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)

更新

我想我弄清楚你要问的是什么.如果你有一个WrapPanel由行铺设,并希望将其强制到下一行,你可以取代单WrapPanel

 <StackPanel Orientation="Vertical">
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
  <StackPanel Orientation="Horizontal">
   <!-- items -->
  </StackPanel>
 </StackPanel>
Run Code Online (Sandbox Code Playgroud)

如果要保留单个行的换行,可以WrapPanel在垂直内部使用s StackPanel.


小智 13

public class NewLine : FrameworkElement
{
    public NewLine()
    {
        Height = 0;
        var binding = new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(WrapPanel), 1),
            Path = new PropertyPath("ActualWidth")
        };
        BindingOperations.SetBinding(this, WidthProperty, binding);
    }
}

<WrapPanel>
    <TextBox Text="Text1"/>
    <TextBox Text="Text2"/>
    <my:NewLine/>
    <TextBox Text="Text3"/>
    <TextBox Text="Text4"/>
</WrapPanel>
Run Code Online (Sandbox Code Playgroud)

  • 在答案中至少包含代码的简要说明总是更好. (5认同)
  • 这确实应该是公认的答案。正是 OP 想要的。Jay 的解决方案只是一个解决方法。 (3认同)