title: Binding usercontrol inside itemcontrol
body: <p>I have 2 tasks.</p>
  
  <ol>
  <li>Add a single usercontrol to a parent window.</li>
  <li>Add a collection of a usercontrol to a parent window.</li>
  </ol>
  
  <p>I have problem to fulfill task 2 in relation to the data binding and command binding.</p>
  
  <p>if someone knows how to do task 2, please add some code.
  This is my implementation for both tasks, in case someone want to fix it.. :</p>
  
  <p>I have a usercontrol called "Book" that contains 3 textblocks and a button.
  The userControl has dependecyProperty of my book model and for the button command.</p>
  
  <p>Book.xaml</p>
  
  <pre><code> &lt;UserControl x:Name="MyBookControl"
      &lt;Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor,  
      AncestorType={x:Type Controls:BookControl}}, Path=TheBook}"&gt;
     &lt;Label Grid.Row="0"&gt;Title&lt;/Label&gt;
          &lt;Label Grid.Row="1"&gt;Author&lt;/Label&gt;
          &lt;Label Grid.Row="2"&gt;Description&lt;/Label&gt;
  
          &lt;TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/&gt;
          &lt;TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/&gt;
          &lt;TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"/&gt;
       &lt;Button Grid.Row="3" Command="{Binding 
      SomeCommand,ElementName=MyBookControl}" Content="Save" /&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>Book.xaml.cs</p>
  
  <pre><code>public partial class BookControl : UserControl
      {
          public BookControl()
          {
              InitializeComponent();
          }
  
          public BookModel TheBook
          {
              get { return (BookModel)GetValue(TheBookProperty); }
              set { SetValue(TheBookProperty, value); }
          }
          public static DependencyProperty TheBookProperty = DependencyProperty.Register("TheBook", typeof(BookModel), typeof(BookControl));
  
          public ICommand SomeCommand
          {
              get { return (ICommand)GetValue(SomeCommandProperty); }
              set { SetValue(SomeCommandProperty, value); }
          }
  
          public static readonly DependencyProperty SomeCommandProperty =
              DependencyProperty.Register("SomeCommand", typeof(ICommand), typeof(BookControl), new UIPropertyMetadata(null));
      }
  </code></pre>
  
  <p>BookModel.cs</p>
  
  <pre><code> public class BookModel
      {
          public string Title { get; set; }
          public string Author { get; set; }
          public string Description { get; set; }
      }
  </code></pre>
  
  <p>In order to complete task 1 I created a window:
  BookWindow</p>
  
  <pre><code>&lt;Window
         DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
  &gt;
  &lt;StackPanel&gt;
  &lt;Controls:BookControl TheBook="{Binding Book}" SomeCommand="{Binding 
      SaveCommand}" /&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>BookViewModel.cs</p>
  
  <pre><code>    public BookModel Book { get; set; }
   public MainViewModel()
          {
              Book = new BookModel{Title = "A Book", Author = "Some Author",   
              Description = "Its a really good book!"};
          }
  
       private ActionCommand _SaveCommand;
              public ICommand SaveCommand
              {
                  get
                  {
                      if (_SaveCommand == null)
                      {
                          _SaveCommand = new ActionCommand(OnSaveCommand, CanSaveCommand);
                      }
                      return _SaveCommand;
                  }
              }
              protected virtual void OnSaveCommand()
              {
                  MessageBox.Show("save clicked");
              }
  
              protected virtual bool CanSaveCommand()
              {
                  return true;
              }
  </code></pre>
  
  <p>Great, Task 1 Completed
  <a href="https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!116&amp;authkey=!AHiyrfEnBr2a-rM&amp;v=3&amp;ithint=photo%2cpng" rel="nofollow">https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!116&amp;authkey=!AHiyrfEnBr2a-rM&amp;v=3&amp;ithint=photo%2cpng</a></p>
  
  <p>Now,  trying to complete task 2:</p>
  
  <p>ContainerWindow:</p>
  
  <pre><code>&lt;Window 
    DataContext="{Binding Source={StaticResource Locator}, Path=Container}"
          &gt;
    &lt;StackPanel&gt;
          &lt;ItemsControl ItemsSource="{Binding Books}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Vertical" /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Controls:BookControl  /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
  
          &lt;/ItemsControl&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>ContainerViewModel.cs : </p>
  
  <pre><code>  private ObservableCollection&lt;BookModel&gt; books;
          public ObservableCollection&lt;BookModel&gt; Books
          {
              get
              {
                  if (books == null)
                  {
                      // Not yet created.
                      // Create it.
                      books = new ObservableCollection&lt;BookModel&gt;();
                  }
  
                  return books;
              }
          }
  
          public ContainerViewModel()
          {
              BookModel book1 = new BookModel { Title = "A Book 2", Author = "Some Author", Description = "Its a really good book!" };
              BookModel book2 = new BookModel { Title = "A Book 3", Author = "Some Author", Description = "Its a really good book!" };
              Books.Add(book1);
              Books.Add(book2);
          }
  </code></pre>
  
  <p>The Binding fail, the button "save" stops respoding.
  <a href="https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!121&amp;authkey=!AKnyQk6Ge_9QHug&amp;v=3&amp;ithint=photo%2cpng" rel="nofollow">https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!121&amp;authkey=!AKnyQk6Ge_9QHug&amp;v=3&amp;ithint=photo%2cpng</a></p>
  
  <p>So, what is going on ? why binding fail, why the button "save" is not functioning ? </p>