Search code examples
wpfrichtextboxwidthstackpanel

A WPF RichTextBox + Button in a StackPanel => what a mess?


This code:

<StackPanel Orientation="Horizontal">
                <RichTextBox />
                <Button Content="Dialog" />  
            </StackPanel>

shows the button somewhere on the left side of the StackPanel ONTO the RichTextBox, WHY?

edit: Oh I just saw its a width problem. The RTB has nearly no width and the button is righthand of it.

edit: seems I run into this bug: WPF RichTextBox with no width set

solution does not work for me!


Solution

  • You'd be better off using a Grid and the HorizontalAlignment (and VerticalAlignment) properties of the RTB.

    <Grid HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <RichTextBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <Button Grid.Column="1" 
                Content="Dialog" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Bottom" />
    </Grid>