我使用 "\u00A0" 作为不间断空格的替代品。我需要一个同样的替代方案来处理连字符(破折号)。在C#中,有什么最好的解决方案吗?
我尝试添加建议的字符,但是"?"代替了"-"。在调试过程中,我可以看到转换期间的"-",但最终却变成了"?"。
我在其他帖子中发现,"-"并不是实际的破折号,可能会导致"?"而不是破折号!(关于"-"和"?"的帖子)我需要一个解决方案,因为我需要一个不间断的破折号...
我尝试添加建议的字符,但是"?"代替了"-"。在调试过程中,我可以看到转换期间的"-",但最终却变成了"?"。
我在其他帖子中发现,"-"并不是实际的破折号,可能会导致"?"而不是破折号!(关于"-"和"?"的帖子)我需要一个解决方案,因为我需要一个不间断的破折号...
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;
}