How can I hide a Paragraph in a FlowDocument?

故事扮演 提交于 2019-12-04 00:09:52

问题


Is there any way to use databinding to show or hide a Paragraph within a FlowDocument? (I want to use MVVM, but with a FlowDocument as my view.)

Paragraph doesn't have a Visibility property. I'm not sure what else to look for.


回答1:


I tried Chris Bova's answer, but it had a couple problems:

  1. Text selection didn't work right
  2. 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:

  1. Name the flow document (ie flowDocument)
  2. Name the item before the paragraph you want to hide (ie previousBlock)
  3. 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);
            }
        }



回答2:


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.




回答3:


Options I can think of...

  1. Hide the content of the paragraph (don't include the paragraph in your model)
  2. Extend Paragraph (or one of its base classes) and provide a dependency property for IsVisible


来源:https://stackoverflow.com/questions/1254570/how-can-i-hide-a-paragraph-in-a-flowdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!