How to exchange the text of a cell of a table that is inside a Flowdocument that is in a Richtextbox

Asked

Viewed 149 times

3

I am trying the following code to replace the text in the question situation, where rtb is the richtextbox, .Document is the flowdocument. The text is exchanged in Run (r), but when I open the document the exchange was not made.

foreach (Block oBlock in rtb.Document.Blocks)
{
    Paragraph oParagraph = oBlock as Paragraph;
    if (oParagraph != null)
    {
        foreach (Inline il in oParagraph.Inlines)
        {
            if (il is Figure)
            {
                foreach (Block bl in ((System.Windows.Documents.AnchoredBlock)(il)).Blocks)
                {
                    if (bl is Table)
                    {
                        Table t = bl as Table;

                        foreach (Block cellBlock in t.RowGroups[reportReference.RowGroup].Rows[reportReference.Line].Cells[reportReference.Column].Blocks)
                        {
                            Paragraph p = cellBlock as Paragraph;
                            if (p != null)
                            {
                                Run r = p.Inlines.FirstInline as Run;
                                if (r != null && r.Text.Contains(searchEntry))
                                {
                                    r.ContentStart.DeleteTextInRun(searchEntry.Length + 1);
                                    r.ContentStart.InsertTextInRun(Constants.TagId + newName);;
                                }   
                            }
                        }

                    }
                }
            }
        }
    }
}

Any suggestions on how to do this?

  • 1

    @mgibsonbr Windows Presentation Foundation.

  • I managed to do what I needed after breaking the face a lot, created a function for the search and then added in the function that called replace using the created references. I just don’t know how to post here, the code is too big

  • 2

    Can you please answer your own question with the solution for the benefit of the community?

1 answer

1

I found the code above difficult to understand, for a quick analysis, and transcribed it in link. Replaces the above code by:

var colecao = rtb.Document
                 .Blocks
                 .OfType<Paragraph>() //recupera os blocos do tipo Paragraph
                 .SelectMany(p=>p.Inlines.OfType<Inline>().Where(il=>il is Figure)) //Recupera os inline do tip Figure do Paragraph acima
                 .SelectMany(li=> ((System.Windows.Documents.AnchoredBlock)(il)).Blocks.OfType<Table>()) //Faz a conversão das figuras e recupera os blocos do tipo Table
                 .SelectMany(t=>t.RowGroups[reportReference.RowGroup].Rows[reportReference.Line].Cells[reportReference.Column].Blocks) //Recup os Blocks das celulas das linhas do grupo
                 .OfType<Paragraph>() //Recupera os do Tipo Paragraph
                 .Select(p=>p.Inlines.FirstInline as Run)
                 .Where(r=>r!=null&& r.Text.Contains(searchEntry))  //Executa o filtro para manter a colecao somente com os objetos necessarios
                 .ToArray();

foreach(var r in colecao){

     r.ContentStart.DeleteTextInRun(searchEntry.Length + 1);
     r.ContentStart.InsertTextInRun(Constants.TagId + newName);;
  }   

If it provides any improvement put here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.