This works fine, and correctly inserts non-breaking spaces into the string:
<TextBlock Text="Non Breaking Text Here"></TextBlock>
But what I really need is to replace spaces with non-breaking spaces during data binding. So I wrote a simple value converter that replaces spaces with " 
". It does indeed replace spaces with " 
" but " 
" is displayed literally instead of showing as a non-breaking space. This is my converter:
public class SpaceToNbspConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value.ToString().Replace(" ", " "); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion }
Does anybody know why it works in XAML, but not in code?
XAML language is treated as XML, it ignores all white spaces. Therefore, if you need to add a value that contains white spaces at the end of it for some reason, it would be ignored. In order to overcome this problem you can use thexml:space=”preserve” in your element declaration.
When you insert the character between such words, it will render a space and will never let any of the words break into a new line.
Special characters may be inserted into DITA content using Unicode character escapes. Your authoring tool may allow the insertion of non-breaking spaces between words. Non-breaking spaces are intended for use when the separation of two adjacent words through line wrapping will result in a loss of meaning or legibility.
The character entity is used to denote a non-breaking space which is a fixed space.
In code the syntax for escaping Unicode chars is different than in XAML:
XAML:   C#: \x00A0
So this should have worked in code:
return value.ToString().Replace(" ", "\xA0");
Have you tried return value.ToString().Replace(' ', System.Convert.ToChar(160));
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With