Salim
Salim

Reputation: 505

WPF textbox max width

I would like the MyTextBox to eat up the full width. The size of the label will change depending on the selected language and expand the first column. The second column of the grid includes the MyTextBox and should expand accordingly to eat the full width. I tried the HorizontalAlignment="Stretch but it seems that the size is defined according the first row of the second column (stack panel). Any idea?

<TabControl Grid.Row="1"  Margin="0 4 0 0" >
                    <TabItem Style="{DynamicResource MenuLevel2}" Header="Online Meeting">
                        <Grid HorizontalAlignment="Stretch" Width="Auto">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="32" />
                                <RowDefinition Height="32" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Label VerticalAlignment="Center" Grid.Row="0" Grid.Column="0">Default Passcode</Label>
                            <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" Margin="10 0 0 0" HorizontalAlignment="Stretch" Width="Auto" >
                                <TextBox MaxLength="100"  horizontalAlignment="Stretch" Margin="0 0 10 0"/>
                                <CheckBox  VerticalAlignment="Center" HorizontalAlignment="Stretch" Click="chkBox_OMRandomPIN_Click"/>
                                <Label  Content="Random" Margin="0 1 0 0" Width="56" HorizontalAlignment="Stretch" />
                            </StackPanel>
                        <Label Grid.Row="1" Grid.Column="0"  Content="Default location label" VerticalAlignment="Center" />
                            <TextBox Grid.Row="1" Grid.Column="1" MaxLength="100" Name="MyTextBox" Margin="11,3,0,3" Height="25" HorizontalAlignment="Stretch" />
                        </Grid>
                    </TabItem>

Upvotes: 0

Views: 3045

Answers (1)

Andy
Andy

Reputation: 12276

You've got too many auto widths in there.

In particular, the column the stackpanel goes in.

This works for me:

    <TabControl Grid.Row="1"  Margin="0 4 0 0" >
        <TabItem  Header="Online Meeting">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="32" />
                    <RowDefinition Height="32" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label VerticalAlignment="Center" Grid.Row="0" Grid.Column="0">Default Passcode</Label>
                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" Margin="10 0 0 0" HorizontalAlignment="Stretch">
                    <TextBox MaxLength="100" HorizontalAlignment="Stretch" Margin="0 0 10 0"/>
                    <CheckBox  VerticalAlignment="Center" HorizontalAlignment="Stretch" Click="chkBox_OMRandomPIN_Click"/>
                    <Label  Content="Random" Margin="0 1 0 0" Width="56" HorizontalAlignment="Stretch" />
                </StackPanel>
                <Label Grid.Row="1" Grid.Column="0"  Content="Default location label" VerticalAlignment="Center" />
                <TextBox Grid.Row="1" Grid.Column="1" MaxLength="100" Name="MyTextBox" Margin="11,3,0,3" Height="25"  />
            </Grid>
        </TabItem>

Things to particularly notice.

I removed Auto off the width of the stackpanel and made the second column * rather than auto

Upvotes: 3

Related Questions