Domanda

I am working with some RichTextBlock objects that contain InlineUIContainer elements. I would like to be able to select and copy all of the text including the text contained in the InlineUIContainer.

Currently, when I select all of the text in the block, the text contained in the InlineUIContainer objects are skipped.

Here is an example of what I'm creating:

<RichTextBlock IsTextSelectionEnabled="True">
    <Paragraph FontSize="20">
        <Bold>This text is selectable</Bold>
        <InlineUIContainer FontFamily="Global User Interface">
            <StackPanel Orientation="Horizontal">
                <TextBlock FontSize="11" VerticalAlignment="Top" Margin="0,0,-1,0">Super Script Text</TextBlock>
                <HyperlinkButton ClickMode="Release" Style="{StaticResource NoMarginHyperlinkButtonStyle}">
                    Link
                </HyperlinkButton>
            </StackPanel>
        </InlineUIContainer>
        This text is also selectable
    </Paragraph>
</RichTextBlock>

If I select all of the text from this piece of Xaml and copy/paste it in NotePad, I don't get the Super Script Text or the Link text.

Is there any way to get all of the text selected?

È stato utile?

Soluzione

This is because HyperlinkButton is not part of the document API and in fact a UIElement wrapped in InlineUIContainer. There are 2 ways to handle this.

  1. Switch to Windows 8.1 and Hyperlink which inherits from TextElement and copy will work just fine.
  2. This is the hard way, if you must support this in Windows 8. Remove the default Context menu items for the RichTextBlock and replace with your own Copy Command. Which should get the 2 TextPointers i.e. RichtextBlock.SelectionStart and RichTextBlock.SelectionEnd

Now with WPF we could get a TextRange within this range but winRT does not expose it, so you will need to do it in your code... Get all the block within the RichTextBlock, and iterate through each to check if it's ContentStart and ContentEnd is within the RTB.SelectionStart and RTB.SelectionEnd if so then add them to a list.

Now it should be easy to extract all the Runs and Bold/Italics from this list and any InlineUIContainers hosting HyperlinkButtons.

2 This is not really a good way to go as it will be hard to allow for margins etc. on Paragraphs etc.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top