MVVM Binding in Silverlight3 ItemsControl to get the parent controls DataContext

Posted by BigTundra on Stack Overflow See other posts from Stack Overflow or by BigTundra
Published on 2010-04-01T19:06:21Z Indexed on 2010/04/01 22:43 UTC
Read the original article Hit count: 662

I have the following ItemsControl in Silverlight 3.

<ItemsControl ItemsSource="{Binding ValueCollectionList}">
  <ItemsControl.ItemTemplate>
      <DataTemplate>

          <Button x:Name="MyBtn" Height="40" Content="{Binding Name}" 
              Tag="{Binding Value}"
              cmd:ButtonBaseExtensions.Command="{Binding ElementName=LayoutRoot, Path=ButtonCommand}"
              cmd:ButtonBaseExtensions.CommandParameter="{Binding ElementName=MyBtn, Path=Tag}"/>

      </DataTemplate>
   </ItemsControl.ItemTemplate>
 </ItemsControl>

The Problem is that I have the ItemsControl bound to the Collection in my ViewModel, but I need the button to trigger a command on the ViewModel which is of course not Available in the DataContext of the button since it only contains the collection.

I can make the command fire by setting my ViewModel as a Resource and then binding to it as a StaticResource, but I want to know why the element to element binding won't work in this scenario. I would prefer not to use the StaticResource binding because that requires the default constructor on the ViewModel and so I can't inject my data easily.

© Stack Overflow or respective owner

Related posts about silverlight-3.0

Related posts about mvvm