title: listbox wraps properly when parented in a vertical orientation stackpanel, but fails for horizontal
body: <p>The following XAML explains what I am trying to do. The listbox wraps as I desire when the parents orientation is set to vertical, but I desire the popout on the left, and then the listbox wrapping behaves unexpectedly. I've seen other posts about binding to parent's size, using value converters etc to do the math, but I am not convinced. Why would/should changing a container 2 parents above suddenly break the listboxes logic?</p>
  
  <pre><code>    &lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:System="clr-namespace:System;assembly=mscorlib"
      Title="MainWindow" Height="421" Width="602"&gt;
  
  &lt;Window.Resources&gt;
  
      &lt;ObjectDataProvider x:Key="OrientationEnum" MethodName="GetValues" ObjectType="{x:Type System:Enum}"&gt;
          &lt;ObjectDataProvider.MethodParameters&gt;
              &lt;x:Type TypeName="Orientation"/&gt;
          &lt;/ObjectDataProvider.MethodParameters&gt;
      &lt;/ObjectDataProvider&gt;
  
      &lt;Style x:Key="HorizontalWrapListBox" TargetType="ListBox"&gt;
          &lt;Style.Setters&gt;
              &lt;Setter Property="ItemsPanel"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;WrapPanel Orientation="Horizontal" /&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
  
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="ListBox"&gt;
                          &lt;ScrollViewer HorizontalScrollBarVisibility="Disabled" &gt;
                              &lt;ItemsPresenter /&gt;
                          &lt;/ScrollViewer&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
  
          &lt;/Style.Setters&gt;
      &lt;/Style&gt;
  
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;StackPanel Name="stackpanel1" Orientation="Vertical" &gt;
          &lt;Expander  &gt;
              &lt;Grid Background="#FFFFFDEB" &gt;
                  &lt;StackPanel Width="200" &gt;
                      &lt;Slider Name="slider1" Minimum="50" Maximum="250"/&gt;
                      &lt;ComboBox ItemsSource="{Binding Source={StaticResource OrientationEnum}}" 
                                SelectedItem="{Binding ElementName=stackpanel1,Path=Orientation}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/Expander&gt;
          &lt;Grid Name="parentingGrid"&gt;
          &lt;ListBox HorizontalAlignment="Stretch" DataContext="{Binding ElementName=slider1}" Style="{StaticResource HorizontalWrapListBox}" &gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
              &lt;Rectangle Width="{Binding Value}" Height="{Binding Value}" Fill="#FFAA5353" Margin="4" /&gt;
          &lt;/ListBox&gt;
          &lt;/Grid&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>