这工作正常,并正确地将不间断的空格插入到字符串中:
<TextBlock Text="Non Breaking Text Here"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
但我真正需要的是在数据绑定期间用不间断的空格替换空格.所以我写了一个简单的值转换器,用"  " 替换空格.它确实用"  " 替换空格,但"  "字面显示而不是显示为不间断的空格.这是我的转换器:
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
}
Run Code Online (Sandbox Code Playgroud)
有人知道它为什么在XAML中工作,但不在代码中吗?