Lennart
Lennart

Reputation: 10333

Align Left and fill space in DockPanel

I have a DockPanel, containing a Label with a fixed width and a TextBox with a Min and MaxWidth. I want to align the TextBox to the left and have it stretch until its MaxWidth and have it shrink until its MinWidth (if the Window is resized for example) Like this:

enter image description here

Panel:

<DockPanel LastChildFill="True">
   <Label Content="Fixed Width"  DockPanel.Dock="Left" />
   <TextBox MinWidth="80" MaxWidth="250" DockPanel.Dock="Left" HorizontalAlignment="Left" />
 </DockPanel>

However this only aligns the TextBox on the left. If I leave out the HorizontalAlignment it centers. How can I achieve something like in the screenshot?

Please note that I don't want to use a Grid, since I need to wrap the TextBox at some point below the Label (I'll do this with a Trigger changing the Dock property currently).

Upvotes: 0

Views: 1110

Answers (1)

Amaury Lev&#233;
Amaury Lev&#233;

Reputation: 1512

Have a look at this solution: https://stackoverflow.com/a/13040678/4625433

You will need to move the MinWidth="80" from the TextBox to the LeftStretchPanel.

<ScrollViewer HorizontalScrollBarVisibility="Auto">
    <DockPanel LastChildFill="True" Background="Yellow">
        <Label Content="Fixed Width" Background="Red" />
        <local:LeftStretchPanel MinWidth="80">
            <TextBox MaxWidth="250" Background="Blue" />
        </local:LeftStretchPanel>
    </DockPanel>
</ScrollViewer>

Upvotes: 1

Related Questions