How can I hide a Paragraph in a FlowDocument?
I tried Chris Bova's answer, but it had a couple problems:
- Text selection didn't work right
- The text inside didn't flow like a paragraph
My solution was to add and remove the paragraph from the flow document.
The steps are:
- Name the flow document (ie flowDocument)
- Name the item before the paragraph you want to hide (ie previousBlock)
- Name the paragraph you want to hide (ie hideParagraph)
Then:
if (<hide paragraph>)
{
if (previousBlock.NextBlock == hideParagraph)
{
flowDocument.Blocks.Remove(hideParagraph);
}
}
else
{
if (previousBlock.NextBlock != hideParagraph)
{
flowDocument.Blocks.InsertAfter(previousBlock, hideParagraph);
}
}
I had the exact same problem and handled it successfully by wrapping the content of the ListItem in a InlineUIContainer, like so:
<ListItem>
<Paragraph>
<InlineUIContainer>
<TextBlock x:Name="HideMe" Visibility="Collapsed">
<Hyperlink NavigateUri="...">Components</Hyperlink>
</TextBlock>
</InlineUIContainer>
</Paragraph>
</ListItem>
From here you can set the visbility of "HideMe" in code or through a binding.