简体   繁体   中英

Find the RichTextBlockOverflow where the paragraph resides UWP

After the answer to my question yesterday I wrote this code but it goes wrong because the "paragraph" is not the "children" of the RichTextBlockOverflow but the first RichTextBlock (I think this is the reason). So I would like to know if it is possible to know in which RichTextBlockOverflow is the paragraph of the "paragraphlist", for example, number 2.

The error that is generated is: System.NullReferenceException: 'Object reference not set to an instance of an object.' and is generated at the click of the btnParent Button (Example 2).

Xaml:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid x:Name="Menù" Width="290" Padding="0" Margin="40">
            <Grid Background="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="50"/>
                </Grid.RowDefinitions>
                <Button  Grid.Row="0" x:Name="btnLoadText" Click="btnLoadText_Click" Content="Display text" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/>
                <Button Grid.Row="1" x:Name="btnParent" Click="btnParent_Click" Content="Parent Name" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/>
                <Button Grid.Row="2" x:Name="btnCount" Click="btnCount_Click" Content="Count" HorizontalAlignment="Center" VerticalAlignment="Center" Width="270" Foreground="White" Height="32"/>
            </Grid>
        </Grid>
        <Grid x:Name="BaseGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Width="720" Height="350">
            <ScrollViewer x:Name="PageViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible" VerticalScrollMode="Disabled" HorizontalScrollMode="Enabled" Background="LightGreen">
                <StackPanel x:Name="StackViewer" Orientation="Horizontal"/>
            </ScrollViewer>
        </Grid>
    </StackPanel>
</Grid>

Code:

public sealed partial class MainPage : Page
{
    List<Paragraph> paragraphlist = new List<Paragraph>();
    RichTextBlock TextOneRich = new RichTextBlock() { Margin = new Thickness(20) };
    List<RichTextBlockOverflow> TextList = new List<RichTextBlockOverflow>();

    public MainPage()
    {
        this.InitializeComponent();

        StackViewer.Children.Add(TextOneRich);
        TextOneRich.Width = 200;
        TextOneRich.TextAlignment = TextAlignment.Justify;
        TextOneRich.Name = "RichTextOne";
    }

    private async void btnLoadText_Click(object sender, RoutedEventArgs e)
    {
        TextList.Clear();
        TextOneRich.Blocks.Clear();
        StackViewer.Children.Clear();
        StackViewer.Children.Add(TextOneRich);

        Paragraph paragraphONE = new Paragraph();
        paragraphONE.Inlines.Clear();
        paragraphONE.Inlines.Add(new Run { Text = "ONE Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." });
        paragraphlist.Add(paragraphONE);

        Paragraph paragraphTWO = new Paragraph();
        paragraphTWO.Inlines.Clear();
        paragraphTWO.Inlines.Add(new Run { Text = "TWO Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." });
        paragraphlist.Add(paragraphTWO);

        Paragraph paragraphTHREE = new Paragraph();
        paragraphTHREE.Inlines.Clear();
        paragraphTHREE.Inlines.Add(new Run { Text = "THREE Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus, nascetur ridiculus mus Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus." });
        paragraphlist.Add(paragraphTHREE);

        int ThereIsTexts = 0;
        int CountName = 1;
        for (int i = 0; i <= ThereIsTexts; i++)
        {
            if (i == 0)
            {
                TextOneRich.Blocks.Add(paragraphONE);
                TextOneRich.Blocks.Add(paragraphTWO);
                TextOneRich.Blocks.Add(paragraphTHREE);

                await Task.Delay(300);

                if (TextOneRich.HasOverflowContent)
                {
                    TextList.Add(new RichTextBlockOverflow() { Width = 200, Margin = new Thickness(20) });
                    StackViewer.Children.Add(TextList[TextList.Count - 1]);
                    TextOneRich.OverflowContentTarget = TextList[TextList.Count - 1];
                    TextList[TextList.Count - 1].Name = "RichOverflo" + CountName.ToString();
                    CountName += 1;
                    ThereIsTexts += 1;
                }
            }
            else
            {
                await Task.Delay(300);
                if (TextList[TextList.Count - 1].HasOverflowContent)
                {
                    TextList.Add(new RichTextBlockOverflow() { Width = 200, Margin = new Thickness(20) });
                    StackViewer.Children.Add(TextList[TextList.Count - 1]);
                    TextList[TextList.Count - 2].OverflowContentTarget = TextList[TextList.Count - 1];
                    TextList[TextList.Count - 1].Name = "RichOverflo" + CountName.ToString();
                    CountName += 1;
                    ThereIsTexts += 1;
                }
            }
        }
    }

    private async void btnParent_Click(object sender, RoutedEventArgs e)
    {
        //// *** Exmple 1 work ***
        //RichTextBlock parent = (paragraphlist[2] as Paragraph).ElementStart.Parent as RichTextBlock;
        //string parentName = (parent as RichTextBlock).Name;
        //var messageDialog = new MessageDialog("The parent is: " + parentName);
        //await messageDialog.ShowAsync();


        //// *** Example 2 not work ***
        RichTextBlockOverflow parent = (paragraphlist[2] as Paragraph).ElementStart.Parent as RichTextBlockOverflow;
        string parentName = (parent as RichTextBlockOverflow).Name;
        var messageDialog = new MessageDialog("The parent is: " + parentName);
        await messageDialog.ShowAsync();
    }

    private async void btnCount_Click(object sender, RoutedEventArgs e)
    {
        var messageDialog = new MessageDialog("The number of Bloks is: " + TextOneRich.Blocks.Count);
        await messageDialog.ShowAsync();
    }
}

Is it possible to find the name of the RichTextBlockOverflow where paragraph number 2 resides?

Thanks in advance!

This should work in your case:

RichTextBlockOverflow parent = paragraphlist[2].ElementStart.VisualParent as RichTextBlockOverflow;

In general, a paragraph may occupy multiple RichTextBlockOverflow elements and a RichTextBlock . If you need to find all of them, then the first one and the last one would be:

var firstOccupiedBlock = paragraph.ElementStart.VisualParent;
var lastOccupiedBlock = paragraph.ElementEnd.VisualParent;

And RichTextBlockOverflow elements between them may be found by tracing the way of OverflowContentTarget property of chained RichTextBlockOverflow elements starting from the firstOccupiedBlock .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM