Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i make the textboxes expand to fill the remaining space of the Grid Cell?

Tags:

wpf

xaml

I have the following window with some input textboxes. But these textboxes will not expand to fill the remaining space of the second column. Furthermore when the window resizes the textboxes doesn't resize accordingly,

Here is my window

enter image description here

Here is my XAML markup

<Window x:Class="WpfApplication8.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         Title="MainWindow" Height="350" Width="525">      <Grid ShowGridLines="True">         <Grid.ColumnDefinitions>             <ColumnDefinition Width="100"></ColumnDefinition>             <ColumnDefinition Width="*"></ColumnDefinition>         </Grid.ColumnDefinitions>         <Grid.RowDefinitions>             <RowDefinition Height="auto"></RowDefinition>             <RowDefinition Height="auto"></RowDefinition>             <RowDefinition Height="auto"></RowDefinition>             <RowDefinition Height="auto"></RowDefinition>             <RowDefinition Height="*"></RowDefinition>             <RowDefinition Height="28"></RowDefinition>         </Grid.RowDefinitions>          <Label Content="First Name" Grid.Column="0" Grid.Row="0"></Label>         <Label Content="Last Name" Grid.Column="0" Grid.Row="1"></Label>         <Label Content="Street Name" Grid.Column="0" Grid.Row="2"></Label>         <Label Content="Suburb" Grid.Column="0" Grid.Row="3"></Label>         <Label Content="City" Grid.Column="0" Grid.Row="4"></Label>          <TextBox Width="313" Grid.Column="1" Margin="3" HorizontalAlignment="Left"/>         <TextBox Width="313" Grid.Column="1" Grid.Row="1" Margin="3"                   HorizontalAlignment="Left" ></TextBox>         <TextBox Width="313" Grid.Column="1" Grid.Row="2" Margin="3"                   HorizontalAlignment="Left"></TextBox>         <TextBox Width="313" Grid.Column="1" Grid.Row="3" Margin="3"                   HorizontalAlignment="Left"></TextBox>         <TextBox Width="313" Grid.Column="1" Grid.Row="4" Margin="3"                  HorizontalAlignment="Left"></TextBox>          <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="5"                      HorizontalAlignment="Right">         <Button Content="Save" Grid.Column="1" Grid.Row="5" Width="100" Margin="3" />         <Button Content="Exit" Grid.Column="1" Grid.Row="5" Width="100"                   HorizontalAlignment="Right" Margin="3"></Button>         </StackPanel>         <!--<TextBox Width="313" Grid.Column="1"></TextBox>-->     </Grid> </Window> 
  1. Is there away to expand the textboxes to fill the remaining space in the second column?
  2. Is there away to make the textboxes resize with the form resize?
like image 330
Phill Greggan Avatar asked Aug 12 '14 15:08

Phill Greggan


People also ask

What is a StackPanel WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.

How many UI containers are available with WPF?

User Interface Panels. There are six panel classes available in UI scenarios: Canvas, DockPanel, Grid, StackPanel, VirtualizingStackPanel, and WrapPanel.


2 Answers

You have the Width hardcoded, so it is always going to stay the same. Remove it, and change the alignment to stretch

<TextBox Grid.Column="1" Margin="3" HorizontalAlignment="Stretch"> 
like image 197
d.moncada Avatar answered Oct 08 '22 19:10

d.moncada


Just a note, if somebody facing with the same problem:

For me the problem was that I use the SharedSizeGroup on the grid for both of my 2 columns. If i deleted the sharedsizegroup="b" on the columns what is *, the problem solved.

<StackPanel Orientation="Vertical"             Grid.IsSharedSizeScope="True">                         <Grid Margin="0 10">                             <Grid.ColumnDefinitions>                                 <ColumnDefinition Width="Auto" SharedSizeGroup="a" />                                 <ColumnDefinition Width="*"  **SharedSizeGroup="b"**/>                             </Grid.ColumnDefinitions>                             <TextBlock Text="Size (m): " />                             <TextBox x:Name="RealObjSize"                                      Grid.Column="1"                                      MinWidth="50"                                      HorizontalAlignment="Stretch"                                      TextChanged="RealObjSize_OnTextChanged" />                         </Grid>                          <Grid Margin="0 10">                             <Grid.ColumnDefinitions>                                 <ColumnDefinition Width="Auto" SharedSizeGroup="a" />                                 <ColumnDefinition Width="*" **SharedSizeGroup="b"**/>                             </Grid.ColumnDefinitions>                             <TextBlock Text="Distance (m): " />                             <TextBox x:Name="RealObjDist"                                  Grid.Column="1"                                  MinWidth="50"                                  HorizontalAlignment="Stretch"                                  TextChanged="RealObjDist_OnTextChanged" />                         </Grid>                     </StackPanel> 
like image 22
Bence Végert Avatar answered Oct 08 '22 18:10

Bence Végert