title: WPF Grid content not spanning the full width
body: <p><strong>SUMMARY</strong></p>
  
  <p>To make it clearer for future readers, here is a summary of the problem and its fix.</p>
  
  <p>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. </p>
  
  <p>The problem was that the style for the tabitem contained a content template that displayed the page through a contentpresenter, <em>inside</em> a horizontal stackpanel. The stackpanel was overriding the stretch properties of the grid in the page.</p>
  
  <p><strong><em>The solution</em></strong> was to replace the stackpanel in the style with a grid. No more layout problems.</p>
  
  <p><strong>ORIGINAL QUESTION</strong></p>
  
  <p>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 <em>that</em> value to <strong>something</strong> that  has the correct value, but I have tried direct binding as well, and no luck.</p>
  
  <p>Here is my xaml:</p>
  
  <pre><code>&lt;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}}"&gt;
  &lt;Page.Resources&gt;
      &lt;converters:IntegerToBooleanConverter x:Key="IntegerToBooleanConverter"&gt;&lt;/converters:IntegerToBooleanConverter&gt;
  &lt;/Page.Resources&gt;
  
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
          &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid Grid.Row="0"&gt;
          &lt;ComboBox ItemsSource="{Binding PageModel.PageUser.AssociatedCustomers}" Name="CbCustomerName" 
                    SelectedItem="{Binding PageModel.PageCustomer}"
                    Style="{StaticResource HeadingTwoComboBox}" cpihelpers:ComboBoxHelper.MaxDropDownItems="10"&gt;
              &lt;ComboBox.ItemContainerStyle&gt;
                  &lt;Style TargetType="ComboBoxItem"&gt;
                      &lt;Setter Property="LayoutTransform" Value="{StaticResource Transform.5x}"&gt;&lt;/Setter&gt;
                      &lt;Setter Property="Foreground" Value="Black"&gt;&lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/ComboBox.ItemContainerStyle&gt;
          &lt;/ComboBox&gt;
      &lt;/Grid&gt;
      &lt;Grid Name="GridMainContent" Grid.Row="1"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="20"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid Name="GridGeneralCustomerInfo" Grid.Column="0" &gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="5"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height=".35*"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height="1*"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height=".8*"&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;StackPanel Name="SpCustomerNotes" Orientation="Vertical" Grid.Column="0" Grid.Row="1"
                          HorizontalAlignment="Left"&gt;
                  &lt;Label Name="LblCustomerNotes" Target="{Binding ElementName=TxtCustomerNotes}" Style="{StaticResource PromptText}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="What is special about {0}?"&gt;&lt;/Label&gt;
                  &lt;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}"&gt;&lt;/TextBox&gt;
              &lt;/StackPanel&gt;
              &lt;Grid Grid.Column="0" Grid.Row="2"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=A few questions about \{0\}...}"&gt;&lt;/TextBlock&gt;
                  &lt;StackPanel Name="SpReportingType" Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblReportingType" Target="{Binding ElementName=TxtReportingType}" Style="{StaticResource PromptText}" Content="Contractual or actual reporting?"&gt;&lt;/Label&gt;
                      &lt;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}"&gt;&lt;/ComboBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpReportingLevel" Orientation="Vertical" Grid.Row="2" Grid.Column="0" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblReportingLevel" Target="{Binding ElementName=TxtReportingLevel}" Style="{StaticResource PromptText}" Content="How should we roll up data?"&gt;&lt;/Label&gt;
                      &lt;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}"&gt;&lt;/ComboBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpReportingPounds" Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblReportingPounds" Target="{Binding ElementName=TxtReportingPounds}" Style="{StaticResource PromptText}" Content="Net or gross weights?"&gt;&lt;/Label&gt;
                      &lt;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}"&gt;&lt;/ComboBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpInventoryLevel" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblInventoryLevel" Target="{Binding ElementName=TxtInventoryLevel}" Style="{StaticResource PromptText}" Content="Customer or title?"&gt;&lt;/Label&gt;
                      &lt;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}"&gt;&lt;/ComboBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpReportByPO" Orientation="Vertical" Grid.Row="2" Grid.Column="1" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblReportByPO" Target="{Binding ElementName=TxtReportByPO}" Style="{StaticResource PromptText}" Content="Do we report by PO?"&gt;&lt;/Label&gt;
                      &lt;CheckBox Name="ChkReportByPO" Style="{DynamicResource SliderCheckBox}" HorizontalAlignment="Left" Checked="ChkReportByPO_Checked"
                                IsChecked="{Binding PageModel.PageCustomer.CustomerInfo.IS_REPORTED_BY_PO, Converter={StaticResource IntegerToBooleanConverter}}"&gt;&lt;/CheckBox&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
              &lt;Grid Name="GridMiscellaneous" Grid.Column="0" Grid.Row="3"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="5"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="5"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Row="0" Text="Other stuff"&gt;&lt;/TextBlock&gt;
                  &lt;StackPanel Name="SpSaveDirectory" Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblSaveDirectory" Target="{Binding ElementName=TxtSaveDirectory}" Style="{StaticResource PromptText}" Content="Where should saved reports go?"&gt;&lt;/Label&gt;
                      &lt;TextBox Name="TxtSaveDirectory" &gt;&lt;/TextBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpFrequency" Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0"&gt;
                      &lt;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}?"&gt;&lt;/Label&gt;
                      &lt;TextBox Name="TxtFrequency" &gt;&lt;/TextBox&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Name="SpHoursPerReport" Orientation="Vertical" Grid.Row="5" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblHoursPerReport" Target="{Binding ElementName=TxtHoursPerReport}" Style="{StaticResource PromptText}" Content="How many hours do you spend per report (whole hours)?"&gt;&lt;/Label&gt;
                      &lt;TextBox Name="TxtHoursPerReport" &gt;&lt;/TextBox&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
          &lt;Grid Name="GridCustomerPeopleAndTitles" Grid.Column="2"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="1.2*"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height="1.5*"&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition Height=".8*"&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid Name="GridCustomerPeople" Grid.Row="0"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock Name="TxtbCustomerPeople" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=These people also work with \{0\}}"&gt;&lt;/TextBlock&gt;
                  &lt;StackPanel Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblSalesReps" Style="{StaticResource PromptText}" Content="Sales Reps:"&gt;&lt;/Label&gt;
                      &lt;TextBlock Name="TxtbSalesReps" Text="{Binding PageModel.PageCustomer.SalesReps}" TextTrimming="CharacterEllipsis" MaxWidth="550"&gt;&lt;/TextBlock&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Orientation="Vertical" Grid.Row="2" Margin="6,0,0,0"&gt;
                      &lt;Label Name="LblBillers" Style="{StaticResource PromptText}" Content="Billers:"&gt;&lt;/Label&gt;
                      &lt;TextBlock Name="TxtbBillers" Text="{Binding PageModel.PageCustomer.Billers}" TextTrimming="CharacterEllipsis" MaxWidth="550"&gt;&lt;/TextBlock&gt;
                  &lt;/StackPanel&gt;
                  &lt;StackPanel Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0" HorizontalAlignment="Stretch"&gt;
                      &lt;Label Name="LblCams" Style="{StaticResource PromptText}" Content="Customer Account Managers:"&gt;&lt;/Label&gt;
                      &lt;TextBlock Name="TxtbCams" Text="{Binding PageModel.PageCustomer.CAMs}" TextTrimming="CharacterEllipsis"&gt;&lt;/TextBlock&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
              &lt;Grid Name="GridTitleGroups" Grid.Row="1"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock Name="TxtbTitleGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s titles}"&gt;&lt;/TextBlock&gt;
                  &lt;Grid Grid.Row="1"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="15"&gt;&lt;/ColumnDefinition&gt;
                          &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                          &lt;ColumnDefinition Width="10"&gt;&lt;/ColumnDefinition&gt;
                          &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                          &lt;ColumnDefinition Width="15"&gt;&lt;/ColumnDefinition&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;ListBox Name="ListTitles" ItemsSource="{Binding}" Grid.Column="1" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}"&gt;
  
                      &lt;/ListBox&gt;
                      &lt;Border Grid.Column="3" Style="{StaticResource BlueBorder}"&gt;
                          &lt;Grid Name="GridTitleGroupItems" Grid.Column="3"&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="30"&gt;&lt;/RowDefinition&gt;
                              &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                          &lt;/Grid.RowDefinitions&gt;
                              &lt;Rectangle Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="{StaticResource BrownAccentBrush}" OpacityMask="{StaticResource StrongOpacityMaskBrush}"
                                         ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitlesNew}}"&gt;&lt;/Rectangle&gt;
                              &lt;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}}"&gt;&lt;/TextBlock&gt;
                              &lt;TreeView Name="TreeTitleGroups" ItemsSource="{Binding}" AllowDrop="True" Grid.Row="1" BorderBrush="{x:Null}" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}"&gt;
  
                          &lt;/TreeView&gt;
                      &lt;/Grid&gt;
                      &lt;/Border&gt;
                  &lt;/Grid&gt;
              &lt;/Grid&gt;
              &lt;Grid Name="GridCustomerGroups" Grid.Row="2"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;TextBlock Name="TxtbCustomerGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s customers}"&gt;&lt;/TextBlock&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>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. </p>
  
  <p>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?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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.</p>
  
  <p>Here is an image of my issue. The contents should fill the entire tab. Note that the surrounding grid is sizing <em>down</em> to fit the content, rather than the content sizing <em>up</em> 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. </p>
  
  <p>What I expect it to do, is for the left portion (with the dropdowns) and the right portion (with the sales reps &amp; such) to be equal in width, filling the entire content area of the tab.</p>
  
  <p><img src="https://i.stack.imgur.com/UCJj5.png" alt="whatitdoesbutshouldnt"></p>