title: WPF TreeviewItem Width
body: <p>i am trying to prevent my treeviewitems from overflowing the window size when
  the content is too long.I have tried various approaches (Binding ActualWidth of parent, HorizontalContentAlign, Wrapping into other containers,...) but without success.</p>
  
  <p>I want to achieve 2 things:</p>
  
  <ul>
  <li>All TreeviewItems must fill the remaining space (Stretch)</li>
  <li>I do not want a horizontal scrollbar, instead i want the text within the 
  textbox overflowing.</li>
  </ul>
  
  <p>That's my code so far.</p>
  
  <p>Any ideas??</p>
  
  <p>KR Manu</p>
  
  <p><a href="https://i.stack.imgur.com/yvjzS.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yvjzS.jpg" alt="enter image description here"></a></p>
  
  <pre><code> &lt;TreeView x:Name="tv"&gt;
          &lt;TreeView.Resources&gt;
              &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                  &lt;Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/&gt;
                  &lt;Setter Property="IsTabStop" Value="True"/&gt;
                  &lt;Setter Property="Focusable" Value="False" /&gt;
                  &lt;Setter Property="IsExpanded" Value="True" /&gt;
                  &lt;Setter Property="Background" Value="Transparent"/&gt;
                  &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
                  &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
                  &lt;Setter Property="Padding" Value="1,0,0,1"/&gt;
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type TreeViewItem}"&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition MinWidth="19" Width="Auto"/&gt;
                                      &lt;ColumnDefinition Width="*"/&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition Height="Auto"/&gt;
                                      &lt;RowDefinition/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
  
                                  &lt;!-- Connecting Lines --&gt;
                                  &lt;Rectangle x:Name="HorLn" Margin="9,1,0,0" Height="1" Stroke="Red" SnapsToDevicePixels="True"/&gt;
                                  &lt;Rectangle x:Name="VerLn" Width="1" Stroke="Red" Margin="0,0,1,0" Grid.RowSpan="2" SnapsToDevicePixels="true"/&gt;
                                  &lt;ToggleButton Margin="-1,0,0,0" x:Name="Expander" Visibility="Hidden"  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/&gt;
                                  &lt;Border x:Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"&gt;
                                      &lt;ContentPresenter Grid.Column="1" x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MinWidth="20"/&gt;
                                  &lt;/Border&gt;
                                  &lt;ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1"/&gt;
                              &lt;/Grid&gt;
                              &lt;ControlTemplate.Triggers&gt;
                                  &lt;Trigger Property="IsExpanded" Value="false"&gt;
                                      &lt;Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/&gt;
                                  &lt;/Trigger&gt;
                                  &lt;Trigger Property="HasItems" Value="false"&gt;
                                      &lt;Setter TargetName="Expander" Property="Visibility" Value="Hidden"/&gt;
                                  &lt;/Trigger&gt;
                                  &lt;MultiTrigger&gt;
                                      &lt;MultiTrigger.Conditions&gt;
                                          &lt;Condition Property="HasHeader" Value="false"/&gt;
                                          &lt;Condition Property="Width" Value="Auto"/&gt;
                                      &lt;/MultiTrigger.Conditions&gt;
                                      &lt;Setter TargetName="PART_Header" Property="MinWidth" Value="75"/&gt;
                                  &lt;/MultiTrigger&gt;
                                  &lt;MultiTrigger&gt;
                                      &lt;MultiTrigger.Conditions&gt;
                                          &lt;Condition Property="HasHeader" Value="false"/&gt;
                                          &lt;Condition Property="Height" Value="Auto"/&gt;
                                      &lt;/MultiTrigger.Conditions&gt;
                                      &lt;Setter TargetName="PART_Header" Property="MinHeight" Value="19"/&gt;
                                  &lt;/MultiTrigger&gt;
                                  &lt;Trigger Property="IsSelected" Value="true"&gt;
                                      &lt;Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt;
                                      &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/&gt;
                                  &lt;/Trigger&gt;
                                  &lt;MultiTrigger&gt;
                                      &lt;MultiTrigger.Conditions&gt;
                                          &lt;Condition Property="IsSelected" Value="true"/&gt;
                                          &lt;Condition Property="IsSelectionActive" Value="false"/&gt;
                                      &lt;/MultiTrigger.Conditions&gt;
                                      &lt;Setter TargetName="Bd" Property="Background" Value="Transparent"/&gt;
                                      &lt;Setter Property="Foreground" Value="White"/&gt;
                                  &lt;/MultiTrigger&gt;
                                  &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                      &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt;
                                  &lt;/Trigger&gt;
                              &lt;/ControlTemplate.Triggers&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/TreeView.Resources&gt;
          &lt;TreeView.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid&gt;
                      &lt;TextBox Text="{Binding Mode=OneWay}" /&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/TreeView.ItemTemplate&gt;
      &lt;/TreeView&gt;
  </code></pre>