How to make a WPF TextBox fill all available space between two buttons ?

11,107

Don't set LastChildFill to false and make the TextBox the last child (by moving the element to the bottom in the code).

(Or use a proper control, like a Grid)

Share:
11,107
Demiurg
Author by

Demiurg

Updated on June 04, 2022

Comments

  • Demiurg
    Demiurg almost 2 years

    I'm trying to make a WPF TextBox fill all available space between two buttons. For some reason the code below does not what I'm trying to achieve

    <DockPanel  Height="48" LastChildFill="False">
        <Button DockPanel.Dock="Left">
            <Image Source="Images\large_load.png"></Image>
        </Button>
        <Button DockPanel.Dock="Left">
            <Image Source="Images\large_reload.png"></Image>
        </Button>
        <TextBox Height="24" HorizontalAlignment="Stretch" DockPanel.Dock="Left"></TextBox>
        <Button DockPanel.Dock="Right" Width="48">
            <Image Source="Images\large_delete.png"></Image>
        </Button>
    </DockPanel>
    

    The TextBox is not stretched.

    Another problem is that when text is added, the textbox width increases and eventually it pushes the right button out of the visible space.

  • Demiurg
    Demiurg almost 13 years
    This is not what I want. I want the last control to be a button.
  • Demiurg
    Demiurg almost 13 years
    Are you saying that it is impossible to do with DockPanel or StackPanel ?
  • H.B.
    H.B. almost 13 years
    It does not matter where in the code the control is placed, all that matters is the relatve position and the attached properties, just grab the TextBox and move it down and set LastChildFill back to true. It just stretches the TextBox, nothing more.
  • Demiurg
    Demiurg almost 13 years
    This did work. Thanks. I must say that this LastChildFill is a very weird way to achieve what I need. Thanks anyway.
  • H.B.
    H.B. almost 13 years
    That is why one does not use a DockPanel for a layout like this (or anything for that matter), normally one would take a Grid with 4 columns where one is set to take all space available.