在RichTextBlock WinRT中计算符号

时间:2013-11-25 22:02:33

标签: xaml windows-runtime winrt-xaml windows-8.1 richtextblock

我有一个带有一些文本的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); } 

2 个答案:

答案 0 :(得分:0)

确保您没有意外地将包装设置为无包装。此外,盒子是否受到约束?

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

<RichTextBlock TextWrapping="Wrap"/>

TextWrapping documentation on msdn

使用您发布的代码:

<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); 
    } 
}

代码包装很好。图像:

enter image description here

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

如果行被限制,那么该框及其内容也将如此。

上次修改,大文字会出现问题。 Large text problem

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

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

结果: enter image description here

你应该可以从这里拿走:)祝你好运。请记住,您需要在视图框中设置一个限制容器来进行包装。

文档:ViewBox

答案 1 :(得分:0)

我想知道RichTextBlockOverflow能否以某种方式帮助你。否则 - 您需要不断添加更多文字,并通过调用Measure() + Arrange()并检查ActualWidth/ActualHeight来查看它何时开始溢出。 如果这听起来很笨 - 您可以使用DirectWrite,这可能更快,更适合处理自定义布局。