Search code examples
wpfxamlflowdocument

Keep a floated element on the same line in a WPF FlowDocument


I have googled for hours and while there are plenty of examples on how to float WPF elements, I am having difficulty getting two simple elements that are floated on the same line. Here is my code

<FlowDocument ColumnWidth="999999">
        <Section>
            <Paragraph>
                <Floater HorizontalAlignment="Left" Width="200">
                    <Paragraph>
                            <Run Text="Hello World Left"/>
                    </Paragraph>
                </Floater>
                <Floater HorizontalAlignment="Right" Width="200">
                    <Paragraph>
                            <Run Text="Hello World Right"/>
                    </Paragraph>
                </Floater>
            </Paragraph>
        </Section>
</FlowDocument>

I would expect these to appear on the same line on the left and right hand side of the page. However the right hand side one gets shifted down by a line:

enter image description here

How can I keep the right hand side floated element at the same height as the left?


Solution

  • Don't know why it works (might do with the hanging or indent), set an empty run as the first Inline of the Paragraph:

                   <Paragraph >
                        <Run /> 
                        <Floater HorizontalAlignment="Left" Background="AliceBlue" 
                                 BaselineAlignment="TextBottom" Width="200">
                            <Paragraph>
                                <Run Text="Hello World Left"/>
                            </Paragraph>
                        </Floater>
                        <Floater HorizontalAlignment="Right" Background="AntiqueWhite" 
                                 BaselineAlignment="TextBottom" Width="200">
                            <Paragraph>
                                <Run Text="Hello World Right"/>
                            </Paragraph>
                        </Floater>
                    </Paragraph>