title: Silverlight 5 Implicit Datatemplate and ContentPresenter Binding MVVM
body: <p>So I have a collection of objects that may be of multiple type. 
  For example a base class with B, and descendents C,D,E. </p>
  
  <p>All this will be in an ItemsControl, however with a custom (somehow random) logic. Therefore I create an ItemsControl in the XAML, specify the DataTemplates binding them to types and let the Silverlight implicit datatemplate engine handle the rest. </p>
  
  <p>The position of the elements inside the ItemsControl presenter is somehow random, and available only at runtime as a dependency property in the element itself (part of the Base class). </p>
  
  <p>My problem is that by default the ItemsControl wraps the items in a ContentPresenter Object, therefore setting the Canvas.Left and Canvas.Top in the data templates has no effect. Because this should be set on the ContentPresenter level instead. However, if I define the ContentPresenter style, the datasource of this is the ItemsControl itself, and not the items. Therefore, I cannot bind that way the positions of the items.</p>
  
  <p>Any ideas, how can I bind the item position properties to the ContentPresenters Canvas.Left property?</p>
  
  <p>In code terms:</p>
  
  <pre><code>&lt;ItemsControl HorizontalAlignment="Stretch" Grid.Row="0" Grid.ColumnSpan="3"
                    x:Name="CardItems" ItemsSource="{Binding CardList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"&gt;
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="SizeChanged"&gt;
                  &lt;ei:CallMethodAction MethodName="WndSizeChanged" TargetObject="{Binding}" /&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;Canvas/&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt;
      &lt;/ItemsControl&gt;
  &lt;UserControl.Resources&gt;
      &lt;DataTemplate DataType="Card:AdCardViewModel"&gt;
          &lt;Canvas d:DesignHeight="300" d:DesignWidth="150" Width="{Binding CardSize.Width}" Height="{Binding CardSize.Height}" Background="Red"&gt;
              &lt;Canvas.Projection&gt;
                  &lt;PlaneProjection RotationY="{Binding Path=Rotation}"/&gt;
              &lt;/Canvas.Projection&gt;
              &lt;Viewbox&gt;
                  &lt;StackPanel&gt;
                      &lt;StackPanel.Background&gt;
                          &lt;SolidColorBrush Color="{StaticResource OriginalRecommendationColor}"/&gt;
                      &lt;/StackPanel.Background&gt;
                      &lt;TextBlock Text="{Binding AdCard}" Foreground="CadetBlue"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Viewbox&gt;
          &lt;/Canvas&gt;
      &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  </code></pre>
  
  <p>PS. I am using the MVVM Light, so I would rather avoid using the code behind file.</p>
  
  <p>LE: </p>
  
  <p>This seems to work, however for some reason the Visual Studio Intellisense has not idea about it. Runtime it seems to be okay, however nothing at design time. Any ideas, how to make all this working in Blend too?</p>
  
  <pre><code>          &lt;ItemsControl.Resources&gt;
              &lt;Style TargetType="ContentPresenter"&gt;
                  &lt;Setter Property="Canvas.Left"  Value="{Binding  Path=Position.Height}"/&gt;
                  &lt;Setter Property="Canvas.Top"  Value="{Binding  Path=Position.Width}"/&gt;
                  &lt;Setter Property="Width" Value="Auto"/&gt;
                  &lt;Setter Property="Height" Value="Auto"/&gt;
                  &lt;Setter Property="HorizontalAlignment" Value="Stretch"/&gt;
                  &lt;Setter Property="VerticalAlignment" Value="Stretch"/&gt;
              &lt;/Style&gt;
          &lt;/ItemsControl.Resources&gt;
  </code></pre>
  
  <p>The viewModelLocator:</p>
  
  <pre><code>    static ViewModelLocator()
      {
          ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default);
  
          // Step #1 - Register the data providing services, design and runtime versions
          if (ViewModelBase.IsInDesignModeStatic)
          {
              SimpleIoc.Default.Register&lt;ICardService, DesignCardService&gt;();
          }
          else
          {
  
              SimpleIoc.Default.Register&lt;ICardService, CardService&gt;();
  
          }
  
          // Step#2 Register the ViewModels
          SimpleIoc.Default.Register&lt;CardSlideShowViewModel&gt;();
      }
  
  
      /// &lt;summary&gt;
      ///     Gets the Video property.
      /// &lt;/summary&gt;
      [SuppressMessage("Microsoft.Performance",
          "CA1822:MarkMembersAsStatic",
          Justification = "This non-static member is needed for data binding purposes.")]
      public CardSlideShowViewModel CardSlideShowSlideShow
      {
          get { return ServiceLocator.Current.GetInstance&lt;CardSlideShowViewModel&gt;(); }
      }
  </code></pre>
  
  <p>Thanks,</p>