C# – Listbox with Custom Listitem — Prevent Selection

clistboxuser-controlswpfwpf-controls

I have a listbox that uses a UserControl as the item template. Inside the UserControl, I have an image (an X) that when clicked, sends out event to remove the UserControl(listitem) from the listbox.

Is there a way to prevent the listbox from selecting that item when a user clicks on the image but still allows listitem selection for everything else in the control?

Best Answer

Make sure you mark the event as handled when the user clicks the Image:

private void ImageClicked(object sender, RoutedEventArgs e)
{
    //send out event to remove UserControl

    //ensure the event doesn't bubble up further to the ListBoxItem
    e.Handled = true;
}