title: How do I Bind WPF Commands between a UserControl and a parent Window
body: <p>I'l start by letting a picture do some talking.</p>
  
  <p><img src="https://i.stack.imgur.com/qmckL.jpg" alt="MVVM User Control to Window wireframe"></p>
  
  <p>So you see, I want to create a WPF user control that supports binding to a parent window's DataContext. The user control is simply a Button and a ListBox with a custom ItemTemplate to present things with a Label and a Remove Button.</p>
  
  <p>The Add button should call an ICommand on the main view model to interact with the user in selecting a new thing (instance of IThing). The Remove buttons in the ListBoxItem in the user control should similarly call an ICommand on the main view model to request the related thing's removal. For that to work, the Remove button would have to send some identifying information to the view model about the thing requesting to be removed. So there are 2 types of Command that should be bindable to this control. Something like AddThingCommand() and RemoveThingCommand(IThing thing).</p>
  
  <p>I got the functionality working using Click events, but that feels hacky, producing a bunch of code behind the XAML, and rubs against the rest of the pristine MVVM implementation. I really want to use Commands and MVVM normally.</p>
  
  <p>There's enough code involved to get a basic demo working, I am holding off on posting the whole thing to reduce confusion. What is working that makes me feel like I'm so close is the DataTemplate for the ListBox binds the Label correctly, and when the parent window adds items to the collection, they show up.</p>
  
  <pre><code>&lt;Label Content="{Binding Path=DisplayName}" /&gt;
  </code></pre>
  
  <p>While that displays the IThing correctly, the Remove button right next to it does nothing when I click it.</p>
  
  <pre><code>&lt;Button Command="{Binding Path=RemoveItemCommand, RelativeSource={RelativeSource AncestorType={x:Type userControlCommands:ItemManager }}}"&gt;
  </code></pre>
  
  <p>This isn't terribly unexpected since the specific item isn't provided, but the Add button doesn't have to specify anything, and it also fails to call the command.</p>
  
  <pre><code>&lt;Button Command="{Binding Path=AddItemCommand, RelativeSource={RelativeSource AncestorType={x:Type userControlCommands:ItemManager }}}"&gt;
  </code></pre>
  
  <p>So what I need is the "basic" fix for the Add button, so that it calls the parent window's command to add a thing, and the more complex fix for the Remove button, so that it also calls the parent command but also passes along its bound thing.</p>
  
  <p>Many thanks for any insights,</p>