Make the ComboBox stretch to free space with maxwidth and right-aligned parent

I have a problem with the layout I want. This is my code:

<DockPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" LastChildFill="True">
                <Label DockPanel.Dock="Left" Content="Add new:"/>
                <Button DockPanel.Dock="Right" Content="Add" VerticalAlignment="Center"/>
                <ComboBox VerticalAlignment="Center" MaxWidth="150" HorizontalAlignment="Stretch">
                    <System:String>Item1</System:String>
                    <System:String>Item2</System:String>
                    <System:String>Item3</System:String>
                </ComboBox>
            </DockPanel>

I want the three elements to be aligned to the right, in the shortcut Order, ComboBox, Button. The shortcut and button should take up as much space as necessary, but I want the ComboBox to occupy the maximum possible space of up to 150 pixels. This works when the DockPanel is not set to HorizontalAlignment = Right.

Any tips / solutions?

Thank.

+3
source share
1 answer

Use the RightToLeft parameter, for example:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid FlowDirection="RightToLeft">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition MaxWidth="150"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
            <Label FlowDirection="LeftToRight" Grid.Column="2" Content="Add new:"/>
            <ComboBox FlowDirection="LeftToRight" Grid.Column="1" VerticalAlignment="Center" MaxWidth="150" HorizontalAlignment="Stretch">
                <ComboBoxItem>Test</ComboBoxItem>
                <ComboBoxItem>Test</ComboBoxItem>
                <ComboBoxItem>Test</ComboBoxItem>
           </ComboBox>
         <Button FlowDirection="LeftToRight"  Grid.Column="0" DockPanel.Dock="Right" Content="Add" VerticalAlignment="Center"/>
     </Grid>
</Grid>

Here it is executed:

Narrow

Wide

+3
source

Source: https://habr.com/ru/post/1792000/


All Articles