0

我在表单上有一个 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);

结果如下所示:

当前图片

但我希望它是这样的:

所需图片

在此处输入图像描述

是否有任何简单的方法可以将段落宽度(或外框大小)设置为等于其内容?

4

2 回答 2

0

你可以这样做,这样边框就看不见了

1.XML

   <Grid>
       <RichTextBox x:Name="TextArea" BorderThickness="0" Padding="0"/>
   </Grid>

2.后面的代码

  TextArea.BorderThickness = new Thickness(0);//No border
  TextArea.Padding = new Thickness(0); //text padding
  TextArea.Margin = new Thickness(10); // margin of txtbox
于 2014-01-27T01:54:22.270 回答
0

TextBox 可以获取自己的内容宽度。RichTextBox 无法获得自己的内容宽度。所以绑定透明的TextBox宽度。但是这个宽度不太正确。你需要更多的设计。(例如,准备复数TextBox?)

<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>
        <TextBox x:Name="ShadowArea" Visibility="Hidden" Padding="5,0" FontSize="20" Height="20" Text="Add same string"/>
        <RichTextBox x:Name="TextArea" Width="{Binding ElementName=ShadowArea, Path=ActualWidth}"/>
    </Grid>
</Window>
于 2017-02-10T14:33:01.190 回答