title: Silverlight: binding a button command within a DataGrid header within an ItemsControl
body: <p>I have a bit of a problem binding a button command to a property of an "outer" data context.</p>
  
  <p>The image below shows the layout I have. I'm trying to bind the CommandParameter of the "Clear" button (outlined in red) to the LocationId. The ItemsControl repeats an ObservableCollection of Locations (see below for Location definition). </p>
  
  <p>The Clear button is basically trying to REMOVE the Addresses attached to the Location, to clear down the DataGrid. To do that I need to pass the LocationId to the ViewModel. </p>
  
  <p>The actual command is triggered perfectly but the binding on the CommandParameter isn't quite right.</p>
  
  <p><img src="https://i.stack.imgur.com/8ZmfC.png" alt="page layout"></p>
  
  <p>Here are the underlying classes of Location &amp; Address:</p>
  
  <pre><code>class Location 
  {   
      int Id;
      ObservableCollection&lt;Address&gt; Addresses;
  }
  
  class Address
  {
      string AddressText;
  }
  </code></pre>
  
  <p>And here is the XAML commented with three alternative attempts &amp; error messages:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Locations, Mode=TwoWay}"&gt;
    &lt;ItemsControl.ItemsPanel&gt;
      &lt;ItemsPanelTemplate&gt;
        &lt;StackPanel VerticalAlignment="Stretch" /&gt;
      &lt;/ItemsPanelTemplate&gt;
    &lt;/ItemsControl.ItemsPanel&gt;
    &lt;ItemsControl.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;TextBlock Text="{Binding Id}"/&gt;
        &lt;sdk:DataGrid x:Name="ResponseDataGrid" ItemsSource="{Binding Addresses}"&gt;
          &lt;sdk:DataGrid.Columns&gt;
            &lt;sdk:DataGridTextColumn Header="Response" Width="*" Binding="{Binding AddressText}"/&gt;
            &lt;sdk:DataGridTemplateColumn Width="100"&gt;
              &lt;sdk:DataGridTemplateColumn.HeaderStyle&gt;
                &lt;Style TargetType="sdk:DataGridColumnHeader"&gt;
                  &lt;Setter Property="ContentTemplate"&gt;
                    &lt;Setter.Value&gt;
                      &lt;DataTemplate&gt;
                        &lt;Button Content="Clear" 
                                Command="{Binding DataContext.ClearLocationCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                                CommandParameter="{Binding Id, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/&gt;
                        &lt;!--System.Windows.Data Error: BindingExpression path error: 'Id' property not found on 'System.Windows.Controls.ItemsControl' --&gt;
  
                        &lt;!--CommandParameter="{Binding ItemsSource.Id, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/&gt;--&gt;
                        &lt;!--System.Windows.Data Error: BindingExpression path error: 'Id' property not found on 'System.Collections.ObjectModel.ObservableCollection`1[UI.Location]' --&gt;
  
                        &lt;!--This gives me the ID but uses a specific index so only works for the first repeated Location--&gt;
                        &lt;!--CommandParameter="{Binding ItemsSource[0].Id, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/&gt;--&gt;
                      &lt;/DataTemplate&gt;
                    &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
                &lt;/Style&gt;
              &lt;/sdk:DataGridTemplateColumn.HeaderStyle&gt;
              &lt;sdk:DataGridTemplateColumn.CellTemplate&gt;
                &lt;DataTemplate&gt;
                  &lt;Button Content="Accept"&gt;
                    &lt;i:Interaction.Triggers&gt;
                      &lt;i:EventTrigger EventName="Click"&gt;
                        &lt;i:InvokeCommandAction 
                          Command="{Binding DataContext.SelectedAddressCommand , RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                          CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType=sdk:DataGrid}}"/&gt;
                      &lt;/i:EventTrigger&gt;
                    &lt;/i:Interaction.Triggers&gt;
                  &lt;/Button&gt;
                &lt;/DataTemplate&gt;
              &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt;
            &lt;/sdk:DataGridTemplateColumn&gt;
          &lt;/sdk:DataGrid.Columns&gt;
        &lt;/sdk:DataGrid&gt;
      &lt;/DataTemplate&gt;
    &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>From the errors, it seems that the button can't quite see the repeated Location object, it can either see the collection of Locations or a specific indexed Location but not the repeated, generated Location that I NEED!</p>
  
  <p>Thanks!</p>