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:
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:
]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.
-
Colin Attwell 96 Reputation points
2021-08-05T09:25:12.007+00:00