Home > Net >  WPF How to make control resize relative to next control
WPF How to make control resize relative to next control

Time:05-02

I have defined the following XAML:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="200" />
            <ColumnDefinition MinWidth="200" />
            <ColumnDefinition MinWidth="500" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Margin="2,2,5,2">
            <GroupBox Header="Computer">
                <DockPanel>
                    <ComboBox MinWidth="100" Name="cmbComputerNames" IsEditable="True" DockPanel.Dock="Left" HorizontalAlignment="Stretch" Width="auto" />
                    <Button Content="Connect" Name="bConnect" Width="65" HorizontalAlignment="Right" />
                </DockPanel>
            </GroupBox>
        </StackPanel>
        <Button Grid.Column="1" Content="Two" Margin="1,2,5,2" />
        <Button Grid.Column="2" Content="Three" Margin="1,2,2,2" />
        <GridSplitter Height="100" Width="4" Grid.Column="0"/>
        <GridSplitter Height="100" Width="4" Grid.Column="1"/>

    </Grid>

So, the left grid column is resizable. I want the "Connect" button to remain right-aligned and with same width. The combobox however, should be left-aligned and the width should grow as the column is resized, so the distance to the connect button remains the same.

Doesn't work: enter image description here

Can anyone tell me how I can achieve that?

CodePudding user response:

Since this is too long for a comment
Replace DockPanel with Grid and try this:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <GroupBox Header="Some text here">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ComboBox MinWidth="100"/>
                <Button Grid.Column="1" Content="A button" Margin="5"/>
            </Grid>
        </GroupBox>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext" Width="10"/>
        <Button Content="some button" Grid.Column="2"/>
    </Grid>

@Andy, if you could produce an answer then I will delete mine.

  •  Tags:  
  • wpf
  • Related