Non breaking space in XAML vs. code
11,014
Solution 1
Have you tried return value.ToString().Replace(' ', System.Convert.ToChar(160));
?
Solution 2
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");
Solution 3
The reason Char is working and string is not - is that the string is escaped when rendered.
Author by
Henrik Söderlund
Updated on June 03, 2022Comments
-
Henrik Söderlund almost 2 years
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?
-
Henrik Söderlund almost 14 yearsPerfect! Thanks! Do you know why it is necessary to work with char rather than string?
-
Erusso87 almost 14 yearsIt should work with the string override too. Just cast the ToChar() to string afterwards.
-
Tore Aurstad over 2 yearsGreat. I needed this to tweak a <Run> inside a <TextBlock>. Thx.