我正在从 VB 中的 Windows 窗体 迁移到 WPF C#,并且编写了以下代码......
namespace TestWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
OutputBox.Document.Blocks.Clear();
}
private void AcceptButton_Click(object sender, RoutedEventArgs e)
{
OutputBox.AppendText(String.Format("You clicked the button.{0}",Environment.NewLine));
}
}
}
其中 OutputBox
是 RichTextBox
。
运行程序并单击按钮几次后,我得到如下输出......
You clicked the button.You clicked the button.
You clicked the button.
You clicked the button.
为什么第一个换行符没有出现,然后它插入空行而不是直接移动到下一行。
如果我将代码更改为
.AppendText(String.format("You clicked the button.{0}OK{0}",environment.NewLine
我得到输出
You clicked the button.
OKYou clicked the button.
OK
You clicked the button.
OK
我的代码应该是什么样子才能获得输出....
You clicked the button.
You clicked the button.
You clicked the button.
最佳答案
使用"\r"
代替Environment.NewLine
。虽然我现在找不到任何链接,但我认为 RichTextBox
处理 Environment.NewLine
关于c# - C# WPF 中奇怪的换行行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365708/