Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non breaking space in XAML vs. code

Tags:

This works fine, and correctly inserts non-breaking spaces into the string:

<TextBlock Text="Non&#160;Breaking&#160;Text&#160;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 "&#160;". It does indeed replace spaces with "&#160;" but "&#160;" 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(" ", "&#160;");     }      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?

like image 973
Henrik Söderlund Avatar asked May 31 '10 13:05

Henrik Söderlund


People also ask

How do I give space in XAML?

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.

How do you code a non-breaking space?

When you insert the &nbsp; character between such words, it will render a space and will never let any of the words break into a new line.

What is non-breaking space in XML?

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.

Which of the following tag is used for nonbreaking spacing?

The &nbsp; character entity is used to denote a non-breaking space which is a fixed space.


2 Answers

In code the syntax for escaping Unicode chars is different than in XAML:

XAML: &#160; C#:   \x00A0 

So this should have worked in code:

return value.ToString().Replace(" ", "\xA0"); 
like image 163
JCH2k Avatar answered Oct 13 '22 05:10

JCH2k


Have you tried return value.ToString().Replace(' ', System.Convert.ToChar(160)); ?

like image 36
bitbonk Avatar answered Oct 13 '22 03:10

bitbonk