C#中连字符的非断行字符是什么?

3
我使用 "\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;
    }
1个回答

10

谢谢!这就是我尝试做的,但出于某种原因 - 在用户界面中该字符变成了“?” :( - MTZ
你能展示相关的代码吗?它在我的 WPF 文本框中可以工作。 - PaulF
代码已添加在上方。谢谢! - MTZ
我不确定 _BindingLayer.RaisePresentationChanged(DependencyPropertyType.Text, textBox.Text); 的含义,但除此之外你的代码在我这里运行得很好(没有问号,破折号的显示也符合预期)。需要注意一点的是,在SetText中,你将_whitespacesText_初始化为带有空格的文本,并立即用连字符替换,所以空格会重新出现 - 你可以用 string whitespacesText = text.Replace(" ", "\u00A0").Replace("-", "\u2011"); 替换两个操作。如果未改变文本框的字体,那么我就无其他建议了。 - PaulF
Arial字体没有这个字符。Times New Roman有。我知道我来晚了,但今天刚刚在解决这个问题 :-) - 0Pat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接