我使用“ \ u00A0”将空格替换为不间断空格。
我需要连字符(破折号)。在C#中,最好的解决方案是什么?
编辑:我试图添加建议的字符,但“?”出现而不是“-”。在调试中,我可以在转换过程中看到“-”,但最终看到的是“?”。
我在其他帖子中发现“-”(不是破折号)可能会导致“?”而不是破折号! (Post regarding "-" and "?")我需要一个解决方案,因为我需要一个不间断的破折号...
public void SetText(string text)
{
if (Mode == ControlMode.DesignTime)
{
string textToUse = string.Empty;
string offlineScreenText = text;
if (offlineScreenText != null)
{
int lblLen = Math.Min(Convert.ToInt32(_settings.MultilineLength), offlineScreenText.Length);
textToUse = lblLen > 0 ? offlineScreenText.Substring(0, lblLen) : offlineScreenText;
}
_textBox.Text = textToUse;
}
else
{
if (!VerifyNumericValidity(text))
{
return;
}
_lastValue = text;
// if there's a length limit, apply it to _lastValue
ApplyLengthLimit();
if (text.Length > _textBox.MaxLength)
{
text = text.Substring(0, _textBox.MaxLength);
}
// Convertion to whitespaces in order to ignore the difference
// between non-breakable space ("\u00A0") and whitespaces (" ")
string whitespacesText = text.Replace(" ", "\u00A0");
whitespacesText = text.Replace("-", "\u2011");
if (!whitespacesText.Equals(_textBox.Text) && !whitespacesText.Equals(_textBox.Text.TrimEnd()))
{
_textBox.Text = text;
}
}
}
void _textBox_TextChanged(object sender, TextChangedEventArgs e)
{
int origCursorLocation = _textBox.SelectionStart;
// Simple whitespeces aren't being wrapped normally,
// so we'll replace them with non-breakable spaces
_textBox.Text = _textBox.Text.Replace(" ", "\u00A0");
_textBox.Text = _textBox.Text.Replace("-", "\u2011");
BindingLayer.RaisePresentationChanged(DependencyPropertyType.Text, _textBox.Text);
_textBox.SelectionStart = origCursorLocation;
}
最佳答案
这应该是您需要的连字符
"\u2011"
这里有特殊字符列表:http://wiki.mobileread.com/wiki/Special_characters