title: WPF - C# - ComboBox/ComboBoxItem: How to stretch text to fill combobox area
body: <p>I am programmatically adding ComboBoxes and ComboBox Items. I have a custom ComboBox Style implemented. Everything is fine except the <strong>text</strong> for each combo box item will not stretch to fill the combobox area. I can get this automatic stretching to work fine with labels and buttons using this bit of XAML in the Custom Style of the button and label controls but it does not have any effect in the ComboBoxItem control.</p>
  
  <pre><code>&lt;Setter Property="ContentTemplate"&gt;
                  &lt;Setter.Value&gt;
                      &lt;DataTemplate&gt;
                          &lt;Viewbox Stretch="Fill"&gt;
                              &lt;ContentPresenter Content="{TemplateBinding Content}"/&gt;
                          &lt;/Viewbox&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
  </code></pre>
  
  <p>Since I am not adding the ComboBoxes in XAML, a solution that involves adding a custom TextBlock will not work (unless it is done dynamically/programmatically). The solution has to either be done in the custom style, or done programmatically in the code behind.</p>
  
  <p><a href="https://i.stack.imgur.com/kiG7s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kiG7s.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;!--ComboBox Template--&gt;
          &lt;ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}"&gt;
              &lt;Grid x:Name="templateRoot" SnapsToDevicePixels="true"&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*"/&gt;
                      &lt;ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" 
                         PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"&gt;
                      &lt;Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}"&gt;
                          &lt;Border x:Name="dropDownBorder" BorderBrush="{StaticResource backgroundColorBrush}" BorderThickness="0" Background="{StaticResource backgroundColorBrush}"&gt;
                              &lt;ScrollViewer x:Name="DropDownScrollViewer"&gt;
                                  &lt;Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled"&gt;
                                      &lt;Canvas x:Name="canvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="0"&gt;
                                          &lt;Rectangle x:Name="opaqueRect" Fill="{StaticResource backgroundColorBrush}"
                                                     Width="{Binding ActualWidth, ElementName=dropDownBorder}"/&gt;
                                      &lt;/Canvas&gt;
                                      &lt;ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                                  &lt;/Grid&gt;
                              &lt;/ScrollViewer&gt;
                          &lt;/Border&gt;
                      &lt;/Themes:SystemDropShadowChrome&gt;
                  &lt;/Popup&gt;
                  &lt;ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" 
                                IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/&gt;
                  &lt;ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                                    Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="Left" 
                                    IsHitTestVisible="false" Margin="0, 0, 0, 0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/&gt;
              &lt;/Grid&gt;
              &lt;ControlTemplate.Triggers&gt;
                  &lt;!--&lt;Trigger Property="HasItems" Value="false"&gt;
                      &lt;Setter Property="Height" TargetName="dropDownBorder" Value="{StaticResource height}"/&gt;
                  &lt;/Trigger&gt;--&gt;
                  &lt;MultiTrigger&gt;
                      &lt;MultiTrigger.Conditions&gt;
                          &lt;Condition Property="IsGrouping" Value="true"/&gt;
                          &lt;Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/&gt;
                      &lt;/MultiTrigger.Conditions&gt;
                      &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false"/&gt;
                  &lt;/MultiTrigger&gt;
                  &lt;Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false"&gt;
                      &lt;Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/&gt;
                      &lt;Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/&gt;
                  &lt;/Trigger&gt;
              &lt;/ControlTemplate.Triggers&gt;
          &lt;/ControlTemplate&gt;
          &lt;!--ComboBox--&gt;
          &lt;Style TargetType="{x:Type ComboBox}"&gt;
              &lt;Setter Property="Background" Value="{StaticResource backgroundColorBrush}"/&gt;
              &lt;Setter Property="BorderBrush" Value="{StaticResource backgroundColorBrush}"/&gt;
              &lt;Setter Property="Foreground" Value="{StaticResource textColorBrush}"/&gt;
              &lt;Setter Property="Width" Value="Auto"/&gt;
              &lt;Setter Property="FontSize" Value="30"/&gt;
              &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="VerticalContentAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/&gt;
              &lt;Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/&gt;
              &lt;Setter Property="Padding" Value="0,0,0,0"/&gt;
              &lt;Setter Property="ScrollViewer.CanContentScroll" Value="False"/&gt;
              &lt;Setter Property="ScrollViewer.PanningMode" Value="None"/&gt;
              &lt;Setter Property="Stylus.IsFlicksEnabled" Value="False"/&gt;
              &lt;Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/&gt;
  
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="IsEditable" Value="true"&gt;
                      &lt;Setter Property="IsTabStop" Value="false"/&gt;
                      &lt;Setter Property="Padding" Value="2"/&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
          &lt;!--ComboBox Item--&gt;
          &lt;Style TargetType="{x:Type ComboBoxItem}"&gt;
              &lt;Setter Property="SnapsToDevicePixels" Value="True"/&gt;
              &lt;Setter Property="Padding" Value="0,0"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="VerticalContentAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
              &lt;Setter Property="Background" Value="Transparent"/&gt;
              &lt;Setter Property="BorderBrush" Value="Transparent"/&gt;
              &lt;!--&lt;Setter Property="FontSize" Value="Auto"/&gt;--&gt;
              &lt;!--&lt;Setter Property="Height" Value="{StaticResource height}"/&gt;--&gt;
              &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;Setter Property="ContentTemplate"&gt;
                  &lt;Setter.Value&gt;
                      &lt;DataTemplate&gt;
                          &lt;Viewbox Stretch="Uniform"&gt;
                              &lt;ContentPresenter Content="{TemplateBinding Content}"/&gt;
                          &lt;/Viewbox&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ComboBoxItem}"&gt;
                          &lt;Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="0" 
                                  SnapsToDevicePixels="true"&gt;
                              &lt;ContentPresenter HorizontalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Stretch"/&gt;
                          &lt;/Border&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="IsEnabled" Value="False"&gt;
                                  &lt;Setter Property="TextElement.Foreground" TargetName="Bd" Value="{StaticResource textColorBrush}"/&gt;
                              &lt;/Trigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="False"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="True"/&gt;
                                      &lt;Condition Property="IsKeyboardFocused" Value="False"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="True"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="False"/&gt;
                                      &lt;Condition Property="IsKeyboardFocused" Value="True"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="True"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="True"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="True"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="False"/&gt;
                                      &lt;Condition Property="IsKeyboardFocused" Value="False"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="False"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="False"/&gt;
                                      &lt;Condition Property="IsKeyboardFocused" Value="True"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                              &lt;MultiTrigger&gt;
                                  &lt;MultiTrigger.Conditions&gt;
                                      &lt;Condition Property="IsSelected" Value="False"/&gt;
                                      &lt;Condition Property="IsMouseOver" Value="True"/&gt;
                                      &lt;Condition Property="IsKeyboardFocused" Value="True"/&gt;
                                  &lt;/MultiTrigger.Conditions&gt;
                                  &lt;Setter Property="Background" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                                  &lt;Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource highlightedColorBrush}"/&gt;
                              &lt;/MultiTrigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
          &lt;!--Used by CheckBoxStyle1--&gt;
          &lt;Style x:Key="OptionMarkFocusVisual"&gt;
              &lt;Setter Property="Control.Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  </code></pre>