can't stretch ComboBox with StackPanel

can't stretch ComboBox with StackPanel

J-Rocks
Collaborator Collaborator
8,777 Views
7 Replies
Message 1 of 8

can't stretch ComboBox with StackPanel

J-Rocks
Collaborator
Collaborator

Hello,

 

I can't stretch ComboBox & TextBox when I add them into StackPanel to be in one line.

I can control the ComboBox if it is outside StackPanel otherwise properites can not effect them while they are in StackPanel.

Is there any solution for this problem?

 

Thanks in advance.

 

    <Grid>
        <StackPanel >
            <StackPanel Orientation="Horizontal">
                <TextBox x:Name="TextBox1" Width="65"></TextBox>
                <ComboBox x:Name="ComboBox1" Height="22" SelectedIndex="0"></ComboBox>
            </StackPanel>            
        </StackPanel>                
    </Grid>  
0 Likes
Accepted solutions (1)
8,778 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

 

Did you try using a Grid instead ?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

_gile
Consultant
Consultant

Something like this:

 

    <Grid>
        <StackPanel >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBox x:Name="TextBox1" Grid.Column="0" Width="65" Margin="5"/>
                <ComboBox x:Name="ComboBox1" Grid.Column="1" Height="22" Margin="5" SelectedIndex="0"></ComboBox>
            </Grid>
        </StackPanel>
    </Grid>

PS: with WPF, it's a good practice to avoid naming the controls and use bindings instead.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 8

J-Rocks
Collaborator
Collaborator

Hi gile,

 

I think it is the same without stretching to right at all.

 

    <Grid>
        <Grid HorizontalAlignment="Left" VerticalAlignment="Top" >
                <TextBox x:Name="TextBox1" Margin="0,0,0,0" Width="65" Height="22"></TextBox>
                <ComboBox x:Name="ComboBox1" Margin="65,0,-175,0" Height="22" SelectedIndex="0" HorizontalAlignment="Stretch"></ComboBox>
       </Grid>
   </Grid> 
0 Likes
Message 5 of 8

_gile
Consultant
Consultant

A Grid is quite useless until you do not define ColumnDefinitions and/or RowDefinitions.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

J-Rocks
Collaborator
Collaborator

Very good ,

 

Binding is something over my head now, and it could be the next thing to learn soon.

 

What's if I waned to add a new TextBox below the first grid and make it stretch as well as the comboBox ?

 

Thank you.

0 Likes
Message 7 of 8

_gile
Consultant
Consultant
Accepted solution

You can use a StackPanel with default Orientation (i.e. Vertical)

 

    <StackPanel >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox x:Name="TextBox1" Grid.Column="0" Width="65" Margin="5"/>
            <ComboBox x:Name="ComboBox1" Grid.Column="1" Height="22" Margin="5" SelectedIndex="0"></ComboBox>
        </Grid>
        <TextBox x:Name="TextBox2" Grid.Column="0" Margin="5" HorizontalAlignment="Stretch"/>
    </StackPanel>

Or a Grid

 

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox x:Name="TextBox1" Grid.Column="0" Width="65" Margin="5"/>
            <ComboBox x:Name="ComboBox1" Grid.Column="1" Height="22" Margin="5" SelectedIndex="0"></ComboBox>
        </Grid>
        <TextBox Grid.Row="1" x:Name="TextBox2" Grid.Column="0" Margin="5"/>
    </Grid>

You should read almost this about WPF containers.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

J-Rocks
Collaborator
Collaborator

Your beautiful examples encouraged me to read and do more examples with Grid ,StackPanel .... Containers in general.

 

Many thanks gile

0 Likes