c# - 通过 XAML 代码在 WPF TextBox 中制作多行文本

标签 c# wpf xaml

1- 将以下代码复制并粘贴到 MainWindow.xaml 文件中。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Button x:Name="Button1" Height="25" Width="100" Content="Press Me" Click="Button1_Click"/>
    <TextBox x:Name="TextBox1" Width="400" Height="200" TextWrapping="Wrap" TextAlignment="Justify" AcceptsReturn="True"/>
</StackPanel>

2- 将以下代码复制并粘贴到代码隐藏文件中。

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
        myStringBuilder.Append("Orange").AppendLine();
        myStringBuilder.Append("").AppendLine();
        myStringBuilder.Append("Apple").AppendLine();
        myStringBuilder.Append("Banana").AppendLine();
        myStringBuilder.Append("").AppendLine();
        myStringBuilder.Append("Plum").AppendLine();
        TextBox1.Text = myStringBuilder.ToString();
    }

3- 运行此项目,然后按 Button1

我的问题:

如何仅通过 XAML 代码管理所需的结果?

(我不想使用 C# 代码)

最佳答案

问题: Newline in string attribute

我相信包含您问题的答案。

<TextBox x:Name="TextBox1" Width="400" Height="200" TextWrapping="Wrap" TextAlignment="Justify" AcceptsReturn="True" Text="A&#x0a;B" />

关于c# - 通过 XAML 代码在 WPF TextBox 中制作多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62206023/

相关文章:

.net - 如何在不违反 MVVM 原则的情况下处理拖放?

c# - 制作一个完全透明的矩形(窗口中的一个洞)WPF

c# - 如何反序列化 protobuf.net 中的 UDPTunnel

c# - 控制台项目和winforms项目在同一个解决方案中

c# - 使用电子邮件地址的 Asp.Net 身份验证

c# - 如何在 Windows 应用商店应用程序中显示大量文本?

c# - 通过按钮在 wpf 窗口中显示用户控件

c# - 将 Func 转换为委托(delegate)

wpf - 如何将焦点设置为 UserControl(使其可选择)?

c# - 如何在一个 Xamarin 表单标签中有 2 个数据绑定(bind)字段?