我在表单上有一个 RichTextBox:
<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">
<Grid>
<RichTextBox x:Name="TextArea"/>
</Grid>
</Window>
在运行时,我使用以下代码添加带边框的段落:
Paragraph p1 = new Paragraph();
Inline hello = new Run("Hello") { FontSize = 14 };
Inline world = new Run("World") { FontSize = 20, Foreground = new SolidColorBrush(Colors.Red) };
Inline helloWorld = new Run(Environment.NewLine + "Hello World");
p1.Inlines.Add(hello);
p1.Inlines.Add(world);
p1.Inlines.Add(helloWorld);
p1.BorderThickness = new Thickness(1);
p1.BorderBrush = new SolidColorBrush(Colors.SkyBlue);
p1.Padding = new Thickness(2);
this.TextArea.Document.Blocks.Add(p1);
结果如下所示:
但我希望它是这样的:
是否有任何简单的方法可以将段落宽度(或外框大小)设置为等于其内容?