question

ColinAttwell-1094 avatar image
0 Votes"
ColinAttwell-1094 asked ColinAttwell-1094 commented

Whitespace NOT being preserved in a WPF TextBox

I read that to defeat the default whitespace handling in a WPF TextBox it is sufficient to include the tag xml:space="preserve". However, in my case, this fails, and the whitespace in my text is NOT RESPECTED during text wrapping. Here is the xaml code:

<UserControl x:Class="EMS_Config_Tool.UIComponents.WPF.EMS_UI_StructText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox xml:space="preserve" Name="StructTextBox" VerticalAlignment="Top" HorizontalAlignment="Left"
LostKeyboardFocus="LostKeyboardFocus" PreviewKeyDown="TextBox_KeyDown" IsHitTestVisible="False"
HorizontalContentAlignment="Left" VerticalContentAlignment="Top" TextWrapping="Wrap" />
</Grid>
</UserControl>

The screen appearance is this:
![120522-image.png][1]



The actual text to be displayed is this:
" Fire Control Panel Hello World"

There are 6 spaces between the words "Panel" and "Hello".

I am trying to emulate the text as it is displayed on a 20x4 LCD display. The user expects to see the formatting on the screen to be a reasonably accurate representation of what is seen on the display.

I have also tried using the <Run Text="" />approach, but this too fails to provide the desired result.

Lastly, I tried replacing the spaces with "non-breaking" spaces, but this also failed as the font I am using does not preserve the fixed character of the font, and the spacing is again incorrect.

I have tried these methods with TextBox AND TextBlock, but neither works as required.

Is there a solution to this issue I have not yet tried, or will I be forced to recreate the component to have TWO text boxes and hand craft the text contents from the input string because there is not a reliable means to preserve the whitespace so that the formatting is correct.

dotnet-csharpwindows-wpf
image.png (4.6 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

huh.... even posting this problem report has the same problem....the whitespace between the words "Panel" and "Hello" is removed in the posting process...... It must be a very terrible thing to have too much whitespace? Perhaps I should call it by a different name so it won't be too offensive.

0 Votes 0 ·

huh.... even posting this problem report has the same problem....the whitespace between the words "Panel" and "Hello" is removed in the posting process.....

Browsers do not display multiple whitespace characters. However, the whitespaces characters do exist in the HTML view. The HTML world has a preformatted text element, <pre>, which displays the text as is including spaces. There is also the non-breaking space character (&nbsp;) which tells the browser to display a space.

I'm not a WPF developer but I took a quick look at the standard docs and XAML has similar features. For example, the XAML non-breaking space encoding is; &#160;


0 Votes 0 ·

Hi,@ ColinAttwell-1094.Where is the problematic text in your picture displayed? And what method is used to display it? Does your LostKeyboardFocus and PreviewKeyDown have any effect on the display of spaces? And are you using the system's default font or other fonts? I use the following code to display spaces normally in the TextBox.

 <TextBox xml:space="preserve" Name="StructTextBox" Width="129" Height="50"  VerticalAlignment="Top" HorizontalAlignment="Left"     IsHitTestVisible="False" TextWrapping="Wrap" >Fire control Panel      Hello World</TextBox>

Am i missing something?

0 Votes 0 ·

This is how it is supposed to appear:
120736-image.png



There are 6 spaces between the words "Panel" and "Hello", as mentioned in the original problem statement. The font is not a MS Windows font, but a third party font the emulates LCD character displays with fixed kerning. Displaying spaces inside strings is not an issue, it is when the TextBox WRAPS the text that the leading spaces in the second line are removed. I seek to defeat the standard manner in which such successions of spaces are removed BY THE SYSTEM IITSELF, in other words, to allow the spaces to be preserved without modification.

0 Votes 0 ·
image.png (13.3 KiB)

Is this textbox editable?


0 Votes 0 ·
Show more comments

1 Answer

ColinAttwell-1094 avatar image
0 Votes"
ColinAttwell-1094 answered

To all who would try to solve this, please be aware that due to commercial pressures I have moved away from what might have seemed like a quick solution to the user requirement, to hand crafting a UserControl to provide all the necessary functionality. The UserControl is generally displayable, and in my instance, I have embedded it into a TreeView control to allow all editable fields for each device (data element) to be viewed and edited as required.
For interest sake, I present the first few nodes in the finished tree, to show how the component appears in context to some other editable fields:
120718-image.png



image.png (26.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.