setting Grid.Row to Auto stack two rows over each other? - wpf

for the second row of the inner Grid I should set its Height explicitly, and if I set it to Auto the second and third rows are stacked over each other (will fill the same row even if the have different Grid.Row values):
<Window x:Class="Notifications.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Notifications"
mc:Ignorable="d"
Title="Fun with Notifications!" Height="225" Width="325" WindowStartupLocation="CenterOwner">
<Grid IsSharedSizeScope="True" Margin="5,0,5,5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="CarLabels"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Vehicle"/>
<ComboBox Name="cboCars" Grid.Column="1" DisplayMemberPath="PetName" />
</Grid>
<Grid Grid.Row="1" DataContext="{Binding ElementName=cboCars, Path=SelectedItem}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="CarLabels"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/> <!--If set to auto, it will stack over the next row-->
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="Id"/>
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=CarId}" />
<Label Grid.Column="0" Grid.Row="1" Content="Make" Grid.RowSpan="2"/>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Make}" Grid.RowSpan="2" />
<Label Grid.Column="0" Grid.Row="2" Content="Color"/>
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Color, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Column="0" Grid.Row="3" Content="Pet Name"/>
<TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Path=PetName}"/>
<StackPanel Grid.Column="1" Grid.Row="4" HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,5,0,5">
<Button x:Name="btnAddCar" Content="Add Car" Margin="5,0,5,0" Padding="4, 2" Click="btnAddCar_Click"/>
<Button x:Name="btnChangeColor" Content="Change Color" Margin="5,0,5,0" Padding="4, 2" Click="btnChangeColor_Click"/>
<Button x:Name="btnRemoveCar" Content="Remove Car" Margin="5,0,5,0" Padding="4,2" Click="btnRemoveCar_Click"/>
</StackPanel>
<Label Grid.Column="0" Grid.Row="5" Content="Is Changed"/>
<CheckBox Grid.Column="1" Grid.Row="5" VerticalAlignment="Center" Margin="10,5,0,5" IsEnabled="False" IsChecked="{Binding Path=IsChanged}" />
</Grid>
</Grid>
</Window>
http://i.imgur.com/hcbVBb5.png

Remove Grid.RowSpan="2" for the Make Label and Textbox. This causes it to run onto the following row.
It should be like this:
<Label Grid.Column="0" Grid.Row="1" Content="Make"/>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Make}"/>

Related

Button is not showing up in WPF Grid

I have a Grid inside of a Grid but when i try to display a button with text, it doesn't show up.
My WPF has a Grid nested inside of a parent Grid.
<Grid Margin="124,0,0,225">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Test" FontSize="14" HorizontalAlignment="Center"/>
<TextBox Width="400" Grid.Column="0" Grid.Row="1" FontSize="14" HorizontalAlignment="Center" KeyDown="HandleEnterKey"/>
<TextBlock Grid.Column="0" Grid.Row="2" Text="Enter Amount" FontSize="14" HorizontalAlignment="Center"/>
<TextBox Width="250" Grid.Column="0" Grid.Row="3" FontSize="14" HorizontalAlignment="Center" KeyDown="HandleEnterKey"/>
<TextBlock Grid.Column="0" Grid.Row="5" FontSize="14" HorizontalAlignment="Center"/>
<Grid
x:Name="chilGrid"
Width="auto"
Height="auto"
Background="Black"
Grid.Column="0"
Grid.Row="7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Button Margin="10, 10, 5, 5" Grid.Column="0" Content="test" Grid.Row="0" Background="Pink"
BorderBrush="Black" BorderThickness="10">
</Button>
</Grid>
</Grid>
Edit# 1:
Modified the button section to this:
<Button Grid.Column="0" Content="test" Grid.Row="0" Background="Pink"
BorderBrush="Black" BorderThickness="1">
</Button>
Still did not work?
`The 'Test' button is not showing up inside the nested Grid?
Your button resides in below grid and it has several values that affect the visibility, like border thickness, margin etc. You can use stackpanel or even simply your existing grid. You don't need column for main grid (for example).
Try below code.
<Grid Margin="124,0,0,225">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Test" FontSize="14" HorizontalAlignment="Center"/>
<TextBox Width="400" Grid.Row="1" FontSize="14" HorizontalAlignment="Center" KeyDown="HandleEnterKey"/>
<TextBlock Grid.Row="2" Text="Enter Amount" FontSize="14" HorizontalAlignment="Center"/>
<TextBox Width="250" Grid.Row="3" FontSize="14" HorizontalAlignment="Center" KeyDown="HandleEnterKey"/>
<TextBlock Grid.Row="5" FontSize="14" HorizontalAlignment="Center"/>
<Grid
x:Name="chilGrid"
Width="auto"
Height="auto"
Background="Black"
Grid.Column="0"
Grid.Row="7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="test" Background="Pink"
BorderBrush="Black" BorderThickness="1">
</Button>
</Grid>
</Grid>
and you get something like below.
It's there, but the combination of your margins, and border thickness means it blends in with the black background of the grid.
Make your border thickness 1, and remove the margin and you will see the button.

WPF Datepicker shows the content of a nearby Textblock

anybody got an idea why the datepicker shows the content of a nearby label? The label which is displayed is in a different grid. When I delete the Labels text and leave it blank the text of the next label of the grid is displayed and so on.
Cant figure out why this happens. Once im hovering over the datepicker with the mouse it shows the correct watermark.
[EDIT] Sorry, following a part of the code.
<StackPanel Grid.Row="1" Grid.Column="0">
<Grid Margin="10,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Beauftragt durch:"
Grid.Row="0" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="OrderedByEmp"
IsEnabled="False"
Text="{Binding OrderedByEmpName}"
Grid.Row="0" Grid.Column="1"
Width="110" Margin="10,2"
/>
<Label Content="Projekt:"
Grid.Row="1" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="Project"
MaxLength="10"
Text="{Binding Project}"
Grid.Row="1" Grid.Column="1"
Width="110" Margin="0,2"
/>
<Label Content="Auftrag:"
Grid.Row="2" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="Order" Text="{Binding Commission}"
MaxLength="8"
Grid.Row="2" Grid.Column="1"
Width="110" Margin="0,2"
/>
</Grid>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="1">
<Grid Margin="10,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Erstellt am:"
Grid.Row="0" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="OrderDate" Text="{Binding OrderDate, Mode=TwoWay,StringFormat='{}{0:dd.MM.yyyy}'}"
IsEnabled="False"
Grid.Row="0" Grid.Column="1"
Width="110" Margin="0,2"
/>
<Label Content="Benötigt bis:"
Grid.Row="1" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<DatePicker x:Name="ExpectedFinishDate"
Grid.Row="1" Grid.Column="1"
Width="110"></DatePicker>
<Label Content="Abteilung:"
Grid.Row="2" Grid.Column="0"
Width="110" Margin="10,2" />
<ComboBox x:Name="Departments" Grid.Row="2" Grid.Column="1">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Bezeichnung}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1">
<Grid Margin="10,10,2,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
VerticalAlignment="Center" HorizontalAlignment="Right">
FLAG:
</TextBlock>
<Image Grid.Row="0" Grid.Column="1" Height="20" Source="/Pictures/NotStartetButton.png" />
<Label Content="Fertiggestellt am:"
Grid.Row="1" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="FinishDatetime"
IsEnabled="False"
Grid.Row="1" Grid.Column="1"
Width="110" Margin="0,2"
/>
<Label Content="Fertiggestellt durch"
Grid.Row="2" Grid.Column="0"
Width="110" Margin="10,2"
HorizontalAlignment="Left" />
<TextBox x:Name="FinishedByEmp"
IsEnabled="False"
Grid.Row="2" Grid.Column="1"
Width="110" Margin="0,2"
/>
</Grid>
</StackPanel>
When I put the datepicker to another spot its working correct.
Tried now for hours to figure out why... but cant find the reason.
Thanks for any suggestion!
I've tried to reproduce the problem in another file and realized that the behavior has changed. Stupidly I left the further research on that problem since I've thought it's located anywhere in the related bindings and just went ahead to come back to that later.
But finally I made so many changes that the problem just disappeared.

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

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
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>
Is there away to expand the textboxes to fill the remaining space in the second column?
Is there away to make the textboxes resize with the form resize?
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">
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>
just put HorizontalAlignment="Stretch" and remove the Width

WPF Grid content not spanning the full width

SUMMARY
To make it clearer for future readers, here is a summary of the problem and its fix.
My grid sits in a page, that is used as the content for a tabitem. At runtime, the grid refused to fill the entire tabitem area, as seen in the screenshots below.
The problem was that the style for the tabitem contained a content template that displayed the page through a contentpresenter, inside a horizontal stackpanel. The stackpanel was overriding the stretch properties of the grid in the page.
The solution was to replace the stackpanel in the style with a grid. No more layout problems.
ORIGINAL QUESTION
I've got a StackPanel with a label and textbox inside a Grid, and I want to bind the width of the textbox to the width of the first two columns in the grid. I've generally been using the approach of binding the textbox width to its parent, and trying to bind that value to something that has the correct value, but I have tried direct binding as well, and no luck.
Here is my xaml:
<Page x:Class="BPC.CPI.Pages.CustomerMaintenance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cpihelpers="clr-namespace:BPC.CPI.ControlHelpers"
xmlns:robertData="clr-namespace:BPC.Robert.DataEncapsulation;assembly=BPC.Robert"
xmlns:robertGlobals="clr-namespace:BPC.Robert.Globals;assembly=BPC.Robert"
xmlns:avalon="clr-namespace:AvalonLambdas;assembly=AvalonLambdas"
xmlns:converters="clr-namespace:BPC.Utilities.ValueConverters;assembly=BPCUtilities"
mc:Ignorable="d"
d:DesignHeight="602" d:DesignWidth="1149"
Title="CustomerMaintenance" Name="ThisPage"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Page.Resources>
<converters:IntegerToBooleanConverter x:Key="IntegerToBooleanConverter"></converters:IntegerToBooleanConverter>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ComboBox ItemsSource="{Binding PageModel.PageUser.AssociatedCustomers}" Name="CbCustomerName"
SelectedItem="{Binding PageModel.PageCustomer}"
Style="{StaticResource HeadingTwoComboBox}" cpihelpers:ComboBoxHelper.MaxDropDownItems="10">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="LayoutTransform" Value="{StaticResource Transform.5x}"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</Grid>
<Grid Name="GridMainContent" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Name="GridGeneralCustomerInfo" Grid.Column="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height=".35*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Name="SpCustomerNotes" Orientation="Vertical" Grid.Column="0" Grid.Row="1"
HorizontalAlignment="Left">
<Label Name="LblCustomerNotes" Target="{Binding ElementName=TxtCustomerNotes}" Style="{StaticResource PromptText}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="What is special about {0}?"></Label>
<TextBox Name="TxtCustomerNotes" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto"
MaxHeight="{Binding Path=ActualHeight, ElementName=SpCustomerNotes, Converter={avalon:LambdaValueConverter (param * 0.65)}}"
Text="{Binding PageModel.PageCustomer.CustomerInfo.CP_NOTES}"></TextBox>
</StackPanel>
<Grid Grid.Column="0" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=A few questions about \{0\}...}"></TextBlock>
<StackPanel Name="SpReportingType" Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingType" Target="{Binding ElementName=TxtReportingType}" Style="{StaticResource PromptText}" Content="Contractual or actual reporting?"></Label>
<ComboBox Name="CbReportingType" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_TYPE_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingTypes}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_TYPE_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportingLevel" Orientation="Vertical" Grid.Row="2" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingLevel" Target="{Binding ElementName=TxtReportingLevel}" Style="{StaticResource PromptText}" Content="How should we roll up data?"></Label>
<ComboBox Name="CbReportingLevel" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_LEVEL_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingLevel}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LEVEL_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportingPounds" Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingPounds" Target="{Binding ElementName=TxtReportingPounds}" Style="{StaticResource PromptText}" Content="Net or gross weights?"></Label>
<ComboBox Name="CbReportingPounds" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_LBS_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingPounds}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LBS_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpInventoryLevel" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0">
<Label Name="LblInventoryLevel" Target="{Binding ElementName=TxtInventoryLevel}" Style="{StaticResource PromptText}" Content="Customer or title?"></Label>
<ComboBox Name="CbInventoryLevel" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="INV_RPT_LEVEL_ID"
ItemsSource="{Binding PageModel.PageSelection.InventoryReportingLevel}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.INV_RPT_LEVEL_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportByPO" Orientation="Vertical" Grid.Row="2" Grid.Column="1" Margin="6,0,0,0">
<Label Name="LblReportByPO" Target="{Binding ElementName=TxtReportByPO}" Style="{StaticResource PromptText}" Content="Do we report by PO?"></Label>
<CheckBox Name="ChkReportByPO" Style="{DynamicResource SliderCheckBox}" HorizontalAlignment="Left" Checked="ChkReportByPO_Checked"
IsChecked="{Binding PageModel.PageCustomer.CustomerInfo.IS_REPORTED_BY_PO, Converter={StaticResource IntegerToBooleanConverter}}"></CheckBox>
</StackPanel>
</Grid>
<Grid Name="GridMiscellaneous" Grid.Column="0" Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Row="0" Text="Other stuff"></TextBlock>
<StackPanel Name="SpSaveDirectory" Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
<Label Name="LblSaveDirectory" Target="{Binding ElementName=TxtSaveDirectory}" Style="{StaticResource PromptText}" Content="Where should saved reports go?"></Label>
<TextBox Name="TxtSaveDirectory" ></TextBox>
</StackPanel>
<StackPanel Name="SpFrequency" Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0">
<Label Name="LblFrequency" Target="{Binding ElementName=TxtFrequency}" Style="{StaticResource PromptText}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="How often do you run reports for {0}?"></Label>
<TextBox Name="TxtFrequency" ></TextBox>
</StackPanel>
<StackPanel Name="SpHoursPerReport" Orientation="Vertical" Grid.Row="5" Margin="6,0,0,0">
<Label Name="LblHoursPerReport" Target="{Binding ElementName=TxtHoursPerReport}" Style="{StaticResource PromptText}" Content="How many hours do you spend per report (whole hours)?"></Label>
<TextBox Name="TxtHoursPerReport" ></TextBox>
</StackPanel>
</Grid>
</Grid>
<Grid Name="GridCustomerPeopleAndTitles" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.5*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Name="GridCustomerPeople" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbCustomerPeople" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=These people also work with \{0\}}"></TextBlock>
<StackPanel Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
<Label Name="LblSalesReps" Style="{StaticResource PromptText}" Content="Sales Reps:"></Label>
<TextBlock Name="TxtbSalesReps" Text="{Binding PageModel.PageCustomer.SalesReps}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="2" Margin="6,0,0,0">
<Label Name="LblBillers" Style="{StaticResource PromptText}" Content="Billers:"></Label>
<TextBlock Name="TxtbBillers" Text="{Binding PageModel.PageCustomer.Billers}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0" HorizontalAlignment="Stretch">
<Label Name="LblCams" Style="{StaticResource PromptText}" Content="Customer Account Managers:"></Label>
<TextBlock Name="TxtbCams" Text="{Binding PageModel.PageCustomer.CAMs}" TextTrimming="CharacterEllipsis"></TextBlock>
</StackPanel>
</Grid>
<Grid Name="GridTitleGroups" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbTitleGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s titles}"></TextBlock>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="15"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="ListTitles" ItemsSource="{Binding}" Grid.Column="1" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}">
</ListBox>
<Border Grid.Column="3" Style="{StaticResource BlueBorder}">
<Grid Name="GridTitleGroupItems" Grid.Column="3">
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="{StaticResource BrownAccentBrush}" OpacityMask="{StaticResource StrongOpacityMaskBrush}"
ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitlesNew}}"></Rectangle>
<TextBlock Name="TxtbNewTitleGroup" Grid.Row="0" Text="Create a New Group" AllowDrop="True" HorizontalAlignment="Center"
VerticalAlignment="Center" Style="{StaticResource GreenAccentText}" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitlesNew}}"></TextBlock>
<TreeView Name="TreeTitleGroups" ItemsSource="{Binding}" AllowDrop="True" Grid.Row="1" BorderBrush="{x:Null}" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}">
</TreeView>
</Grid>
</Border>
</Grid>
</Grid>
<Grid Name="GridCustomerGroups" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbCustomerGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s customers}"></TextBlock>
</Grid>
</Grid>
</Grid>
</Grid>
</Page>
I've tried several recommended work-arounds from various questions and other sites, but nothing is working. I've tried sticking a border over cells I want to bind to and binding to its dimensions, using a label, nesting another grid, x:naming the ColumnDefinition and binding to the ActualWidth and more. Nothing. Works.
This seems like something that should be really simple in wpf, and I'm just missing come crucial, minor piece. I've wasted probably three hours on this. What am I doing wrong?
EDIT:
I just realized that it appears that I'm binding to the width of the entire GridMainContent - I'm not. That was just kind of dummy value that has been replaced with each different solution I've tried.
Here is an image of my issue. The contents should fill the entire tab. Note that the surrounding grid is sizing down to fit the content, rather than the content sizing up to fit the grid. I've defined implicit styles that set HorizontalAlignment=Stretch on every parent element for my content, but nothing appears to be working.
What I expect it to do, is for the left portion (with the dropdowns) and the right portion (with the sales reps & such) to be equal in width, filling the entire content area of the tab.
Try pasting the xaml below as WPF window content, don't use tabs- I've done that and the layout is fine:
Could you confirm is the layout is correct? My best bet is - the parent of the root grid somehow limiting the available space, so the available width is less the the screen's width. Check if all the available space is paint with lime.
XAML:
<Grid Background="Lime">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ComboBox ItemsSource="{Binding PageModel.PageUser.AssociatedCustomers}" Name="CbCustomerName"
SelectedItem="{Binding PageModel.PageCustomer}"
>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="LayoutTransform" ></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</Grid>
<Grid Name="GridMainContent" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Name="GridGeneralCustomerInfo" Grid.Column="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height=".35*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Name="SpCustomerNotes" Orientation="Vertical" Grid.Column="0" Grid.Row="1"
HorizontalAlignment="Left">
<Label Name="LblCustomerNotes" Target="{Binding ElementName=TxtCustomerNotes}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="What is special about {0}?"></Label>
<TextBox Name="TxtCustomerNotes" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding PageModel.PageCustomer.CustomerInfo.CP_NOTES}"></TextBox>
</StackPanel>
<Grid Grid.Column="0" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=A few questions about \{0\}...}"></TextBlock>
<StackPanel Name="SpReportingType" Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingType" Target="{Binding ElementName=TxtReportingType}" Content="Contractual or actual reporting?"></Label>
<ComboBox Name="CbReportingType" SelectedValuePath="REPORTING_TYPE_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingTypes}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_TYPE_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportingLevel" Orientation="Vertical" Grid.Row="2" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingLevel" Target="{Binding ElementName=TxtReportingLevel}" Content="How should we roll up data?"></Label>
<ComboBox Name="CbReportingLevel" SelectedValuePath="REPORTING_LEVEL_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingLevel}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LEVEL_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportingPounds" Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="6,0,0,0">
<Label Name="LblReportingPounds" Target="{Binding ElementName=TxtReportingPounds}" Content="Net or gross weights?"></Label>
<ComboBox Name="CbReportingPounds" SelectedValuePath="REPORTING_LBS_ID"
ItemsSource="{Binding PageModel.PageSelection.ReportingPounds}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LBS_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpInventoryLevel" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0">
<Label Name="LblInventoryLevel" Target="{Binding ElementName=TxtInventoryLevel}" Content="Customer or title?"></Label>
<ComboBox Name="CbInventoryLevel" SelectedValuePath="INV_RPT_LEVEL_ID"
ItemsSource="{Binding PageModel.PageSelection.InventoryReportingLevel}" DisplayMemberPath="NAME"
SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.INV_RPT_LEVEL_ID}"></ComboBox>
</StackPanel>
<StackPanel Name="SpReportByPo" Orientation="Vertical" Grid.Row="2" Grid.Column="1" Margin="6,0,0,0">
<Label Name="LblReportByPo" Target="{Binding ElementName=TxtReportByPO}" Content="Do we report by PO?"></Label>
<CheckBox Name="ChkReportByPo" Style="{DynamicResource SliderCheckBox}" HorizontalAlignment="Left"
></CheckBox>
</StackPanel>
</Grid>
<Grid Name="GridMiscellaneous" Grid.Column="0" Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Other stuff"></TextBlock>
<StackPanel Name="SpSaveDirectory" Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
<Label Name="LblSaveDirectory" Target="{Binding ElementName=TxtSaveDirectory}" Content="Where should saved reports go?"></Label>
<TextBox Name="TxtSaveDirectory" ></TextBox>
</StackPanel>
<StackPanel Name="SpFrequency" Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0">
<Label Name="LblFrequency" Target="{Binding ElementName=TxtFrequency}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="How often do you run reports for {0}?"></Label>
<TextBox Name="TxtFrequency" ></TextBox>
</StackPanel>
<StackPanel Name="SpHoursPerReport" Orientation="Vertical" Grid.Row="5" Margin="6,0,0,0">
<Label Name="LblHoursPerReport" Target="{Binding ElementName=TxtHoursPerReport}" Content="How many hours do you spend per report (whole hours)?"></Label>
<TextBox Name="TxtHoursPerReport" ></TextBox>
</StackPanel>
</Grid>
</Grid>
<Grid Name="GridCustomerPeopleAndTitles" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="1.2*"></RowDefinition>
<RowDefinition Height="1.5*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Name="GridCustomerPeople" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbCustomerPeople" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=These people also work with \{0\}}"></TextBlock>
<StackPanel Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
<Label Name="LblSalesReps" Content="Sales Reps:"></Label>
<TextBlock Name="TxtbSalesReps" Text="{Binding PageModel.PageCustomer.SalesReps}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="2" Margin="6,0,0,0">
<Label Name="LblBillers" Content="Billers:"></Label>
<TextBlock Name="TxtbBillers" Text="{Binding PageModel.PageCustomer.Billers}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0" HorizontalAlignment="Stretch">
<Label Name="LblCams" Content="Customer Account Managers:"></Label>
<TextBlock Name="TxtbCams" Text="{Binding PageModel.PageCustomer.CAMs}" TextTrimming="CharacterEllipsis"></TextBlock>
</StackPanel>
</Grid>
<Grid Name="GridTitleGroups" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbTitleGroups" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s titles}"></TextBlock>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="15"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="ListTitles" ItemsSource="{Binding}" Grid.Column="1" ToolTip="">
</ListBox>
<Border Grid.Column="3">
<Grid Name="GridTitleGroupItems">
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ToolTip=""></Rectangle>
<TextBlock Name="TxtbNewTitleGroup" Grid.Row="0" Text="Create a New Group" AllowDrop="True" HorizontalAlignment="Center"
VerticalAlignment="Center" ToolTip=""></TextBlock>
<TreeView Name="TreeTitleGroups" ItemsSource="{Binding}" AllowDrop="True" Grid.Row="1" BorderBrush="{x:Null}" ToolTip="">
</TreeView>
</Grid>
</Border>
</Grid>
</Grid>
<Grid Name="GridCustomerGroups" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Name="TxtbCustomerGroups" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s customers}"></TextBlock>
</Grid>
</Grid>
</Grid>
</Grid>
I finally found what was causing the content to not size appropriately. Buried in the tab item style, the ContentTemplate was placing all the content inside a horizontal StackPanel. I switched the StackPanel for a Grid, and voila! Correct wpf layout.

WPF Formatting Issues - Automatically stretching and resizing?

I'm very new to WPF and XAML. I am trying to design a basic data entry form. I have used a stack panel holding four more stack panels to get the layout I want. Perhaps a grid would be better for this, I am not sure.
And here is the XAML code that generates it:
<Window x:Class="Test1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="224" Width="536.762">
<StackPanel Height="Auto" Name="stackPanel1" Width="Auto" Orientation="Horizontal">
<StackPanel Height="Auto" Name="stackPanel2" Width="Auto">
<Label Height="Auto" Name="label1" Width="Auto">Patient Name:</Label>
<Label Height="Auto" Name="label2" Width="Auto">Physician:</Label>
<Label Height="Auto" Name="label3" Width="Auto">Insurance:</Label>
<Label Height="Auto" Name="label4" Width="Auto">Therapy Goals:</Label>
</StackPanel>
<StackPanel Height="Auto" Name="stackPanel3" Width="Auto">
<TextBox Height="Auto" Name="textBox1" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox2" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox3" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox4" Width="Auto" Padding="3" Margin="1" />
</StackPanel>
<StackPanel Height="Auto" Name="stackPanel4" Width="Auto">
<Label Height="Auto" Name="label5" Width="Auto">Date:</Label>
<Label Height="Auto" Name="label6" Width="Auto">Patient Phone:</Label>
<Label Height="Auto" Name="label7" Width="Auto">Facility:</Label>
<Label Height="Auto" Name="label8" Width="Auto">Referring Physician:</Label>
</StackPanel>
<StackPanel Height="Auto" Name="stackPanel5" Width="Auto">
<TextBox Height="Auto" Name="textBox5" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox6" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox7" Width="Auto" Padding="3" Margin="1" />
<TextBox Height="Auto" Name="textBox8" Width="Auto" Padding="3" Margin="1" />
</StackPanel>
</StackPanel>
</Window>
What I really want is for the text boxes to stretch equally to fill up the space horizontally. I would also like for the controls in each vertical stackpanel to 'spread out' evenly as the window is resized vertically.
StackPanel always aligns its children against the top or left edge depending upon its orientation. It sounds like what you want is a UniformGrid where your outer StackPanel is. Try this:
<Window>
<UniformGrid Name="stackPanel1" Rows="1">
<StackPanel Name="stackPanel2">
...
</StackPanel>
<StackPanel Name="stackPanel3">
...
</StackPanel>
<StackPanel Name="stackPanel4">
...
</StackPanel>
<StackPanel Name="stackPanel5">
...
</StackPanel>
</UniformGrid>
</Window>
Note that you don't need to set Width=Auto or Height=Auto, this is implied.
But you are right that a Grid is probably better (even though the XAML is ugly) because in this layout configuration your labels could easily get out of alignment with the text boxes. You can try something like this too...
<UniformGrid Rows="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Field 1" />
<TextBox Grid.Row="0" Grid.Column="1" />
<Label Grid.Row="1" Grid.Column="0" Content="Field 2" />
<TextBox Grid.Row="1" Grid.Column="1" />
<Label Grid.Row="2" Grid.Column="0" Content="Field 3" />
<TextBox Grid.Row="2" Grid.Column="1" />
</Grid>
<Grid /> <!-- repeat above -->
<Grid /> <!-- etc... -->
</UniformGrid>
Taking off what Josh said, if you require the controls to fill the space vertically as well, you can define some "space filler" rows to spread out the controls vertically as well. Here's some XAML which demonstrates this (I added margins to push the labels out from the window border).
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Margin="5">Patient Name:</Label>
<TextBox Grid.Column="1" Grid.Row="0" Margin="5" />
<Label Grid.Column="0" Grid.Row="2" Margin="5">Physician:</Label>
<TextBox Grid.Column="1" Grid.Row="2" Margin="5" />
<Label Grid.Column="0" Grid.Row="4" Margin="5">Insurance:</Label>
<TextBox Grid.Column="1" Grid.Row="4" Margin="5" />
<Label Grid.Column="0" Grid.Row="6" Margin="5">Therapy Goals:</Label>
<TextBox Grid.Column="1" Grid.Row="6" Margin="5" />
<Label Grid.Column="2" Grid.Row="0" Margin="5">Date:</Label>
<TextBox Grid.Column="3" Grid.Row="0" Margin="5" />
<Label Grid.Column="2" Grid.Row="2" Margin="5">Patient Phone:</Label>
<TextBox Grid.Column="3" Grid.Row="2" Margin="5" />
<Label Grid.Column="2" Grid.Row="4" Margin="5">Facility:</Label>
<TextBox Grid.Column="3" Grid.Row="4" Margin="5" />
<Label Grid.Column="2" Grid.Row="6" Margin="5">Referring Physician:</Label>
<TextBox Grid.Column="3" Grid.Row="6" Margin="5" />
</Grid>

Resources