Non breaking space in XAML vs. code

…衆ロ難τιáo~ 提交于 2019-12-20 17:32:30

问题


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?


回答1:


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




回答2:


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");



回答3:


The reason Char is working and string is not - is that the string is escaped when rendered.



来源:https://stackoverflow.com/questions/2943767/non-breaking-space-in-xaml-vs-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!