c# - C# WPF 中奇怪的换行行为

标签 c# wpf

我正在从 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));
        }
    }
}

其中 OutputBoxRichTextBox

运行程序并单击按钮几次后,我得到如下输出......

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/

相关文章:

c# - 属性网格编号格式

c# - 如何在 WPF 窗口中显示屏幕保护程序的预览

wpf - 如何触发此错误模板?

c# - 使用 Http.RouteAttribute 路由可选参数

c# - 在 .NET 中,我什么时候必须引用外部库也引用的 DLL?

wpf - 滚动到 DataGrid 底部时出现的行下方的额外空间?

wpf - 如何绑定(bind)到 IronPython 中的 ListBox?

wpf - Caliburn.Micro:ControlTemplate 中按钮的 Action

c# - 如何从 C# 线程获取成员函数作为该线程参数的对象?

c# - EventWaitHandle - WaitAny() 和 WaitOne() 之间的区别