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

Demiurg picture Demiurg · Jul 30, 2011 · Viewed 10.8k times · Source

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.

Answer

H.B. picture H.B. · Jul 30, 2011

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)