title: ListBox with Grid as ItemsPanelTemplate produces weird binding errors
body: <p>I've got a ListBox control and I'm presenting a fixed number of ListBoxItem objects in a grid layout. So I've set my ItemsPanelTemplate to be a Grid.</p>
  
  <p>I'm accessing the Grid from code behind to configure the RowDefinitions and ColumnDefinitions.</p>
  
  <p>So far it's all working as I expect. I've got some custom IValueConverter implementations for returning the Grid.Row and Grid.Column that each ListBoxItem should appear in.</p>
  
  <p>However I get weird binding errors sometimes, and I can't figure out exactly why they're happening, or even if they're in my code.</p>
  
  <p>Here's the error I get:</p>
  
  <blockquote>
    <p><code>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')</code></p>
  </blockquote>
  
  <p>Can anybody explain what's going on?</p>
  
  <p>Oh, and, here's my XAML:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;!-- Value Converters --&gt;
      &lt;v:GridRowConverter x:Key="GridRowConverter" /&gt;
      &lt;v:GridColumnConverter x:Key="GridColumnConverter" /&gt;
      &lt;v:DevicePositionConverter x:Key="DevicePositionConverter" /&gt;
      &lt;v:DeviceBackgroundConverter x:Key="DeviceBackgroundConverter" /&gt;
  
      &lt;Style x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}"&gt;
          &lt;Setter Property="FocusVisualStyle" Value="{x:Null}" /&gt;
          &lt;Setter Property="Background" Value="Transparent" /&gt;
  
          &lt;Setter Property="Grid.Row" Value="{Binding Path=DeviceId, Converter={StaticResource GridRowConverter}}" /&gt;
          &lt;Setter Property="Grid.Column" Value="{Binding Path=DeviceId, Converter={StaticResource GridColumnConverter}}" /&gt;
  
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                      &lt;Border CornerRadius="2" BorderThickness="1" BorderBrush="White" Margin="2" Name="Bd"
                              Background="{Binding Converter={StaticResource DeviceBackgroundConverter}}"&gt;
                          &lt;TextBlock FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" 
                                  Text="{Binding Path=DeviceId, Converter={StaticResource DevicePositionConverter}}" &gt;
                              &lt;TextBlock.LayoutTransform&gt;
                                  &lt;RotateTransform Angle="270" /&gt;
                              &lt;/TextBlock.LayoutTransform&gt;
                          &lt;/TextBlock&gt;
                      &lt;/Border&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsSelected" Value="true"&gt;
                              &lt;Setter TargetName="Bd" Property="BorderThickness" Value="2" /&gt;
                              &lt;Setter TargetName="Bd" Property="Margin" Value="1" /&gt;
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;            
      &lt;/Style&gt;        
  &lt;/UserControl.Resources&gt;
  
  &lt;Border CornerRadius="3" BorderThickness="3" Background="#FF333333" BorderBrush="#FF333333" &gt;
      &lt;Grid ShowGridLines="False"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="15" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;StackPanel Grid.Row="0" Orientation="Horizontal"&gt;
              &lt;Image Margin="20,3,3,3" Source="Barcode.GIF" Width="60" Stretch="Fill" /&gt;
          &lt;/StackPanel&gt;
  
          &lt;ListBox ItemsSource="{Binding}" x:Name="lstDevices" Grid.Row="1" 
                   ItemContainerStyle="{StaticResource DeviceContainerStyle}"
                   Background="#FF333333"
                   SelectedItem="{Binding SelectedDeviceResult, ElementName=root, Mode=TwoWay}" &gt;
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;Grid&gt;
                          &lt;Grid.LayoutTransform&gt;
                              &lt;RotateTransform Angle="90" /&gt;
                          &lt;/Grid.LayoutTransform&gt;                            
                      &lt;/Grid&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p></p>