Okay, I’m not sure this is the best way to do this, but I needed a way to remove an item from an ItemsControl within the UI context of the item itself. My problem was that the item itself, when in a datatemplate, doesn’t know its parent.  The RelativeSource property came to the rescue (tip o’ the hat to Jaime Rodriguez), although my use of the Tag property seems a little funky. Here’s my solution:


<DataTemplate x:Key="DataTemplate1">  
    <StackPanel Orientation="Vertical" >
        <TextBox Text="{Binding Path=DisplayName}"/>
<TextBox Text="{Binding Path=NavigateURL}"/>
<Button Content="Remove" Click="Remove_Click" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}"/> </StackPanel> </DataTemplate>

Then, in the code behind, here’s what I do:


void Remove_Click(object sender, RoutedEventArgs e)  
    Button button = (Button) sender;
    ListBox listBox = (ListBox)button.Tag;
    MenuItemCollection menuItemCollection = (MenuItemCollection)listBox.ItemsSource;

Basically, I get the listbox out of the Tag element and then call into its ItemsSource property, remove the object which is bound to the data template itself.  (Note that MenuItem and MenuItemCollection are objects I created.)

Maybe a little hacky, but it works!