Remove item in a listbox with Button in ListBoxItemTemplate

May 1, 2012 at 9:19 AM

Hi,

I hope someone can help me out here. 

  • I have a button in the DataTemplate for the ListBoxItem.
  • I want to remove the item from the listbox when this button is clicked.

I have tried to use the CallMethodAction and bind the Click event but since the Datacontext for the item in an Interface, where do I put the remove method?

The remove method need to be in the viewmodel where the collection is but the datacontext is the single item. How do I achieve this?

Hope you understand my problem and give me some direction.

Cheers!

May 7, 2012 at 8:20 AM

OK, finally figured this one out as well. Upgrading to Silverlight 5 helped me a lot. Please let me know if anyone knows how we do this in Silverlight 4.

In the DataTemplate:

  • I have a button and I have the trigger to ClickMethodAction
  • The method resides in the viewModel which is the DataContext of the ListBox
  • I set the Target binding of the trigger to the datacontext of AncestorType Listbox
  • and the method to a method - DeleteItem() that removes selectedItem.
  • This worked for me.