title: Width="*" in longlistselector giving error "Error HRESULT E_FAIL has been returned from a call to a COM component"
body: <p>I used DataTemplateSelector example as shown in <a href="https://stackoverflow.com/questions/17821165/longlistselector-different-item-template-for-first-and-last-item">this page</a> and modified it a little to suit my requirements as shown below.</p>
  <pre><code>public abstract class TemplateSelector : ContentControl
  {
      public abstract DataTemplate SelectTemplate(object item, DependencyObject container);
  
      protected override void OnContentChanged(object oldContent, object newContent)
      {
          base.OnContentChanged(oldContent, newContent);
  
          var parent = GetParentByType&lt;LongListSelector&gt;(this);
  
          ContentTemplate = SelectTemplate(newContent, this);
      }
  
      private static T GetParentByType&lt;T&gt;(DependencyObject element) where T : FrameworkElement
      {
          T result = null;
          DependencyObject parent = VisualTreeHelper.GetParent(element);
  
          while (parent != null)
          {
              result = parent as T;
  
              if (result != null)
              {
                  return result;
              }
  
              parent = VisualTreeHelper.GetParent(parent);
          }
  
          return null;
      }
  }
  
  
  public class MyTemplateSelector : TemplateSelector
  {
      public DataTemplate one { get; set; }
      public DataTemplate two { get; set; }
      public DataTemplate three { get; set; }
      public DataTemplate four { get; set; }
      public DataTemplate five { get; set; }
      public DataTemplate six { get; set; }
      public DataTemplate seven { get; set; }
      public DataTemplate eight { get; set; }
      public DataTemplate nine { get; set; }
  
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          PoiData poiItem = item as PoiData;
  
          if (poiItem != null)
          {
              switch (poiItem.Type)
              {
                  case &quot;atm&quot;:
                      return one;
  
                  case &quot;food&quot;:
                      return two;
  
                  case &quot;hospital&quot;:
                      return three;
  
                  case &quot;police&quot;:
                      return four;
  
                  case &quot;pharmacy&quot;:
                      return five;
  
                  case &quot;gas_station&quot;:
                      return six;
  
                  case &quot;hindu_temple&quot;:
                      return seven;
  
                  case &quot;train_station&quot;:
                      return eight;
  
                  case &quot;movie_theater&quot;:
                      return nine;
  
              }
          }
          return null;
      }
  }    
  </code></pre>
  <p>And my xaml looks like this.</p>
  <pre><code>&lt;phone:PhoneApplicationPage.Resources&gt;
  
  &lt;DataTemplate x:Key=&quot;one&quot;&gt;
          &lt;StackPanel Height=&quot;75&quot;&gt;
              &lt;Grid Height=&quot;65&quot;
                    Background=&quot;Red&quot;
                    HorizontalAlignment=&quot;Left&quot;          
                    Margin=&quot;0, 0, 0, 12&quot;&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width=&quot;65&quot; /&gt;
                      &lt;ColumnDefinition Width=&quot;auto&quot; /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid VerticalAlignment=&quot;Top&quot;
                                    HorizontalAlignment=&quot;Left&quot;
                                    Width=&quot;65&quot;
                                    Height=&quot;65&quot;
                                    Background=&quot;White&quot;
                                    Grid.Column=&quot;0&quot;
                                    Margin=&quot;0, 0, 0, 0&quot;&gt;
                  &lt;/Grid&gt;
                  &lt;Grid Grid.Column=&quot;1&quot; Width=&quot;auto&quot;&gt;
                      &lt;TextBlock Text=&quot;{Binding Title}&quot; 
                                 FontSize=&quot;30&quot; Margin=&quot;10,0,0,0&quot; 
                                 VerticalAlignment=&quot;Center&quot; 
                                 Foreground=&quot;White&quot;/&gt;
                  &lt;/Grid&gt;
  
              &lt;/Grid&gt;
          &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  .
  .
  .
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  
  &lt;DataTemplate x:Key=&quot;SelectingTemplate&quot;&gt;
              &lt;local:MyTemplateSelector Content=&quot;{Binding}&quot;
                                        one=&quot;{StaticResource one}&quot;
                                        two=&quot;{StaticResource two}&quot;
                                        three=&quot;{StaticResource three}&quot;
                                        four=&quot;{StaticResource four}&quot;
                                        five=&quot;{StaticResource five}&quot;
                                        six=&quot;{StaticResource six}&quot;
                                        seven=&quot;{StaticResource seven}&quot;
                                        eight=&quot;{StaticResource eight}&quot;
                                        nine=&quot;{StaticResource nine}&quot; /&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  <p>And this is the panorama item xaml</p>
  <pre><code>&lt;phone:PanoramaItem Header=&quot;{Binding Path=LocalizedResources.AroundMe, Source={StaticResource LocalizedStrings}}&quot;&gt;
                  &lt;!--Double wide Panorama with large image placeholders--&gt;
                  &lt;phone:LongListSelector Margin=&quot;12,-20,0,75&quot;
                                          ItemsSource=&quot;{Binding Poi.Items}&quot;
                                          ItemTemplate=&quot;{StaticResource SelectingTemplate}&quot;
                                          SelectionChanged=&quot;PoiSelectionChanged&quot; &gt;
                  &lt;/phone:LongListSelector&gt;
              &lt;/phone:PanoramaItem&gt;
  </code></pre>
  <p>My problem is, if I use this code, the <code>stackpanel</code> always aligns itself to the center of the <code>panorama</code> item no matter if I set <code>HorizontalAlignment</code> to <code>left</code> or <code>right</code> or <code>stretch</code>.</p>
  <p>And if I set the <code>width</code> property of either the <code>stackpanel</code> or the child <code>grid</code> to <code>*</code> to take up the entire screen space, I get an error which I think is completely irrelevant.</p>
  <pre><code>{MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---&gt; System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
  at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
  at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
  at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
  at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth,     Double inHeight, Double&amp; outWidth, Double&amp; outHeight)
  --- End of inner exception stack trace ---}
  </code></pre>
  <p>What exactly am I supposed to do to set the <code>stackpanel</code>'s width to take up the entire screen width ? Is there any workaround ?</p>
  <p>And why is this exception throwing when I set the <code>width</code> to <code>*</code>.</p>