XAML 与代码中的不间断空间 [英] Non breaking space in XAML vs. code

查看:34
本文介绍了XAML 与代码中的不间断空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这工作正常,并且正确地将不间断空格插入到字符串中:

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

<TextBlock Text="Non&#160;Breaking&#160;Text&#160;Here"></TextBlock>

但我真正需要的是在数据绑定期间用不间断空格替换空格.所以我写了一个简单的值转换器,用&#160;"替换空格.它确实用&#160;"替换了空格,但&#160;"是按字面显示的,而不是显示为不间断空格.这是我的转换器:

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
}

有人知道为什么它在 XAML 中有效,而在代码中无效吗?

Does anybody know why it works in XAML, but not in code?

推荐答案

你试过 return value.ToString().Replace(' ', System.Convert.ToChar(160)); 吗?

这篇关于XAML 与代码中的不间断空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆