0

我有一个带有一些文本的 RichTextBlock。我不想使用垂直和水平滚动。如果我添加大文本,则会隐藏一些文本。如何获取隐藏文本或如何获取当前未隐藏的文本?我的 RichTextBlock 的大小动态设置。TextWrapping="Wrap" 已设置。

<RichTextBlock x:Name="BookViewer" HorizontalAlignment="Left" Height="525" Margin="63,0,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="1246" TextAlignment="Justify" > </RichTextBlock> 

代码隐藏:

private void SetText(string value) { BookViewer.Blocks.Clear(); Run myRun = new Run(); myRun.Text = value; Paragraph myParagraph = new Paragraph(); myParagraph.Inlines.Add(myRun); BookViewer.Blocks.Add(myParagraph); } 
4

2 回答 2

0

确保您没有不小心将包装设置为无包装。另外,盒子是否受到任何限制?

默认情况下,文本换行设置为 true:

<RichTextBlock TextWrapping="Wrap"/>

msdn 上的 TextWrapping 文档

使用您发布的代码:

<Page
x:Class="Wrap.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Wrap"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <RichTextBlock x:Name="BookViewer" 
                   HorizontalAlignment="Left" 
                   Height="525" 
                   Margin="63,0,0,0" 
                   Grid.Row="1" 
                   TextWrapping="Wrap" 
                   VerticalAlignment="Top" 
                   Width="1246" TextAlignment="Justify" >
    </RichTextBlock>
</Grid>

和代码隐藏:

    public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        var str = "Bacon ipsum dolor sit amet ball tip tenderloin pork loin, tri-tip beef jowl pork belly capicola beef ribs ham bacon pig. Pork chop ball tip leberkas spare ribs pork belly pastrami shoulder ham hock andouille kielbasa short loin corned beef tenderloin chicken venison. Sausage jowl beef salami spare ribs ball tip. Swine brisket pancetta biltong shankle rump ground round jerky sausage. Rump beef chuck, salami leberkas jowl hamburger pancetta short loin tenderloin swine ground round ham. Meatloaf ball tip flank short loin boudin brisket t-bone pork chop." +
            "Rump tri-tip pork drumstick kielbasa tenderloin shank beef ribs pig prosciutto swine spare ribs meatball ham. Pork pork loin t-bone, chicken turducken pork belly meatloaf bacon shankle sirloin pancetta shank drumstick. Kielbasa doner shoulder turducken sirloin pancetta, venison bacon corned beef beef pork belly shank. Bresaola drumstick short ribs t-bone. Tenderloin biltong salami, swine kielbasa shoulder short loin sirloin turducken capicola. Cow tri-tip jowl ground round short loin tail. Ribeye corned beef ball tip pork belly swine capicola chuck." +
            "Rump tri-tip pork drumstick kielbasa tenderloin shank beef ribs pig prosciutto swine spare ribs meatball ham. Pork pork loin t-bone, chicken turducken pork belly meatloaf bacon shankle sirloin pancetta shank drumstick. Kielbasa doner shoulder turducken sirloin pancetta, venison bacon corned beef beef pork belly shank. Bresaola drumstick short ribs t-bone. Tenderloin biltong salami, swine kielbasa shoulder short loin sirloin turducken capicola. Cow tri-tip jowl ground round short loin tail. Ribeye corned beef ball tip pork belly swine capicola chuck.";
        SetText(str);
    }

    private void SetText(string value) 
    {
        BookViewer.Blocks.Clear(); 
        Run myRun = new Run(); 
        myRun.Text = value; 
        Paragraph myParagraph = new Paragraph();
        myParagraph.Inlines.Add(myRun); BookViewer.Blocks.Add(myParagraph); 
    } 
}

代码包装得很好。图片:

在此处输入图像描述

包装受周围包含的影响,粘贴一些代码,我们可能会提供帮助。

如果行被压缩,盒子和它的内容也会被压缩。

最后编辑,大文本你会遇到问题。 大文本问题

使用 ViewBox 和拉伸属性来获得所需的效果,可能类似于:

        <Viewbox  Grid.Row="1" StretchDirection="DownOnly">
        <RichTextBlock FontSize="65" Width="3000" x:Name="BookViewer">
        </RichTextBlock>
    </Viewbox>

结果: 在此处输入图像描述

你应该可以从这里拿走它:) 祝你好运。请记住,您需要在视图框内设置一个限制容器才能获得包装。

文档:ViewBox

于 2013-11-25T22:50:17.053 回答
0

I wonder if RichTextBlockOverflow could somehow help you there. Otherwise - you'd need to keep adding more text and see when it starts overflowing by calling Measure()+Arrange() and checking ActualWidth/ActualHeight. If that sounds clunky - you could use DirectWrite instead, which might be faster and better suited to handling custom layouts.

于 2013-11-26T01:13:13.967 回答