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