-1

Why won't my TextBox fill the available space in its DockPanel parent? I expected it to stretch to fill the remaining horizontal space. The Button is attached to the right nicely. I've got this at the top of my Window:

<DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" >
    <TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
    <Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
</DockPanel>

The TextBox attaches to the left but is about 5 pixels wide. It's the last child, and has the defaults DockPanel.Dock=Left, HorizontalAlignment=Stretch. I've tried other dock and alignment values without success. Is TextBox an exception to the usual layout rules?

Bob Peterson
  • 636
  • 7
  • 16
  • I copied the wrong permutation of code, I've been trying several including the "correct" answer. Something else is screwing me up. I need to keep looking, – Bob Peterson Feb 21 '19 at 15:17

2 Answers2

2

When you set DockPanel.LastChildFill=true

What is the last child you're adding?

The button.

Not the textbox.

Order is top (first) to bottom (last).

I'm not sure exactly what result you want but maybe you just need to make the textbox the last child:

    <DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" >
        <Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
        <TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
    </DockPanel>
Andy
  • 11,864
  • 2
  • 17
  • 20
  • I copied the wrong permutation to the original question. I had been trying many permutations when I couldn't get it to work as you've shown. Something else is happening wrong -- if I find out then I'll post an update. – Bob Peterson Feb 21 '19 at 15:15
0

This seems to work for me:

<DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" LastChildFill="True">
    <Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
    <TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
</DockPanel>
mami
  • 586
  • 1
  • 4
  • 14