Search code examples
c#wpfflowdocument

Absolute positioning of UIElement in FlowDocument in WPF


I want to add a Button control that appears on top of a Hyperlink control. Currently, the Button is part of a FlowDocument and appears next to the Hyperlink, but I want it to have absolute positioning so that it overlaps the Hyperlink. How can I achieve this?

<FlowDocumentScrollViewer>
    <FlowDocument>
        <Paragraph>
            The maximum speed is
            <Hyperlink>150</Hyperlink>
            <InlineUIContainer>
                <Button>No way!</Button>
            </InlineUIContainer>
            in this road!
        </Paragraph>
    </FlowDocument>
</FlowDocumentScrollViewer>

Solution

  • You can use a StackPanel to "stack" multiple objects that inherit from UIElement. Hyperlink doesn't inherit from UIElement, but you can get around this by placing the Hyperlink inside a ContentControl.

    Here is a working example:

    <FlowDocumentScrollViewer>
        <FlowDocument>
            <Paragraph>
                The maximum speed is
                <StackPanel>
                    <Button>No way!</Button>
                    <ContentControl HorizontalAlignment="Center">
                        <Hyperlink>150</Hyperlink>
                    </ContentControl>
                </StackPanel>
                in this road!
            </Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
    

    Edited code, with Button appearing on top (higher z-order) of the Hyperlink (see comments).

    <FlowDocumentScrollViewer>
        <FlowDocument>
            <Paragraph>
                The maximum speed is
                <Grid>
                    <Button>No way!</Button>
                    <ContentControl HorizontalAlignment="Center">
                        <Hyperlink>150</Hyperlink>
                    </ContentControl>
                </Grid>
                in this road!
            </Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>