WPF: How can I stretch the middle child in a DockPanel? WPF: How can I stretch the middle child in a DockPanel? wpf wpf

WPF: How can I stretch the middle child in a DockPanel?


You need to set DockPanel.Dock attached property for your elements and leave TextBox as the last element:

<RadioButton HorizontalAlignment="Stretch"             HorizontalContentAlignment="Stretch">    <DockPanel LastChildFill="True">        <TextBlock DockPanel.Dock="Left"                   VerticalAlignment="Center"                   Text="in location:" />        <Button DockPanel.Dock="Right"                Margin="10,0,0,0"                Padding="3,0"                Content="..." />        <TextBox Margin="10,0,0,0">            Path string        </TextBox>    </DockPanel></RadioButton>


Accepted answer is fixing your problem but creates another one. If someone uses keyboard (TAB) to navigate through your interface, Button will be focused before TextBox. This could be very annoying on a long run. If you don't want to brake your tab orders use Grid instead of DockPanel:

<RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition Width="Auto"/>            <ColumnDefinition/>            <ColumnDefinition Width="Auto"/>        </Grid.ColumnDefinitions>        <TextBlock Grid.Column="0" VerticalAlignment="Center">in location:</TextBlock>        <TextBox Grid.Column="1" Margin="10,0,0,0">Path string</TextBox>        <Button Grid.Column="2" HorizontalAlignment="Right" Margin="10,0,0,0" Padding="3,0">...</Button>    </Grid></RadioButton>

Alternative would be to control tab orders yourself using TabIndex attribute. This can be tricky though, especially when you display your control in a collection.

<RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TabIndex="0">    <DockPanel LastChildFill="True">        <TextBlock DockPanel.Dock="Left" VerticalAlignment="Center" Text="in location:" />        <Button DockPanel.Dock="Right" Margin="10,0,0,0" Padding="3,0" Content="..." TabIndex="2"/>        <TextBox Margin="10,0,0,0" TabIndex="1">Path string</TextBox>    </DockPanel></RadioButton>