Search code examples
c#wpfrichtextboxinlineuicontainer

Removing InlineUIContainer from WPF RichTextBox


I have an RTB with InlineUIContainer's. I store them in a List, so I can access them directly. How can I remove them from my RTB in C#?

Code example:

// for some TextPointer textPointer in my RTB

TextBlock tb = new TextBlock();
tb.Text = "hello world";

InlineUIContainer inlineUIContainer = new InlineUIContainer(tb, textPointer);
tb_list.Add(inlineUIContainer);

Solution

  • Here, you can remove it like below. If this is your local collection of containers:

                List<InlineUIContainer> containers = new List<InlineUIContainer>();
    

    and you want to remove the container which is first in your list then:

                InlineUIContainer inlineContainer = containers[0] ;    
                foreach (var block in myRTB.Document.Blocks)
                {
                    if (block is Paragraph)
                    {
                        var paragraph = block as Paragraph;
    
                        if (paragraph.Inlines.Contains(inlineContainer))
                        {
                            paragraph.Inlines.Remove(inlineContainer);
                        }
                    }
                }