1

The title pretty much says it all. Paste a string longer than 9600 characters into a TextBox that has TextWrapping set to TextWrapping.NoWrap, and it will split it into 9600-character lines, wrap them, and grow vertically. Is there a good reason for this? Any way to prevent it?

Note that "you shouldn't use TextBox for a string that long" is a valid opinion, but doesn't answer the question. :)

The XAML to demonstrate this doesn't need to be any more complicated than:

<Window x:Class="TestApp.TestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="TestWindow" Height="300" Width="300">
    <TextBox TextWrapping="NoWrap" HorizontalAlignment="Left" VerticalAlignment="Top" />
</Window>
dlf
  • 9,045
  • 4
  • 32
  • 58
  • 1
    How many characters is it with another font size? – Clemens Sep 05 '17 at 20:41
  • @Clemens Font size does not appear to have any effect – dlf Sep 05 '17 at 21:06
  • Man... you _really_ need to look at some form of double-buffering that text, so that you're only loading 1-2k max into that textbox. Otherwise, it is going to be so painful. That being said. I think you should just pop the textbox into a fixed-size scrollviewer and control things from there. – code4life Sep 06 '17 at 00:04

1 Answers1

0

My solution to this was to add MaxLines="1" to the TextBox. This doesn't stop WPF from breaking very long strings into multiple lines, but it does prevent the control from growing vertically when this happens, which means users can't break my layout by pasting the full text of War and Peace into the search box. :)

dlf
  • 9,045
  • 4
  • 32
  • 58