WPF TextBox无法正确显示空格

时间:2015-09-17 13:48:12

标签: c# wpf

首先,我是WPF的新手。我有一些已包含格式化文本的字符串值,例如:

Person        Name = Jack       Age = 40     #male
Person        Name = Bill       Age = 18     #male
Person        Name = Liza       Age = 19     #female
Person        Name = Kate       Age = 38     #female

当我把它写到文件时,一切都很好。但是当我尝试将此字符串传递给textBox时,我看到类似这样的内容:

Person   Name = Jack   Age = 40     #male
Person      Name = Bill     Age = 18 #male
Person   Name = Liza       Age = 19    #female
Person       Name = Kate     Age = 38  #female

对我来说没有意义,为什么我失去了空间。我应该使用RichTextBox吗?我试过但实际上文字以同样的方式显示

这是我的代码。对不起我的低技能编码水平

public override string TransformCollectionToSourceType(IList<ElementPin> elementPins)
    {
        var stringBuilder = new StringBuilder();
        var maxSignalNameLength = elementPins.MaxStringLength(pin => pin.SignalName.Length);
        var maxPinNameLength = elementPins.HasEmptyValue(pin => pin.PinName)
                                   ? EmpyPinName.Length
                                   : elementPins.MaxStringLength(pin => pin.PinName.Length);
        var maxIoStandartLength = elementPins.MaxStringLength(pin => pin.IoStandardValue.Length)
                                  > EmptyIoStandard.Length
                                      ? elementPins.MaxStringLength(pin => pin.IoStandardValue.Length) + 1
                                      : EmptyIoStandard.Length + 1;
        foreach (var elemPin in elementPins)
        {
            var signalName = elemPin.ElementCommented
                                 ? string.IsNullOrWhiteSpace(elemPin.SignalName)
                                       ? EmptySignalName.PadRight(maxSignalNameLength)
                                       : elemPin.SignalName.PadRight(maxSignalNameLength)
                                 : string.IsNullOrWhiteSpace(elemPin.SignalName)
                                       ? WhiteSpace + EmptySignalName.PadRight(maxSignalNameLength)
                                       : WhiteSpace + elemPin.SignalName.PadRight(maxSignalNameLength);
            var pinName = string.IsNullOrWhiteSpace(elemPin.PinName)
                              ? EmpyPinName.PadRight(maxPinNameLength)
                              : elemPin.PinName.PadRight(maxPinNameLength);
            var ioStandardValue = string.IsNullOrWhiteSpace(elemPin.IoStandardValue)
                                      ? (EmptyIoStandard + ";").PadRight(maxIoStandartLength)
                                      : (elemPin.IoStandardValue + ";").PadRight(maxIoStandartLength);
            stringBuilder.AppendFormat(
                "{0}NET {1}   LOC = {2} | IOSTANDARD = {3} # {4}",
                elemPin.ElementCommented ? "#" : string.Empty,
                signalName,
                pinName,
                ioStandardValue,
                string.IsNullOrWhiteSpace(elemPin.Comment) ? EmptyComment : elemPin.Comment);
            stringBuilder.AppendLine();
        }

        return stringBuilder.ToString().RemoveLastNewlineSymbol();
    }

2 个答案:

答案 0 :(得分:3)

空格不适合格式化。尝试标签:

&#34; \吨&#34;

答案 1 :(得分:0)

有完全相同的问题。 使用String.Format进行填充。

请参阅:https://msdn.microsoft.com/en-us/library/aa331875(v=vs.71).aspx

var string1 = "Some left-aligned string with maxWith = 60"; 
var string2 = "Some right-aligned string";   
var output = $"{string1, -60}{string2, 30}"

这没有正确显示。

我可以通过选择等宽字体

来解决问题