0

I have a WPF UserControl that contains a ComboBox. I need to attach an event listener to the ComboBox.Items collection.

public MyUserControl()
{
    InitializeComponent();

    ((INotifyCollectionChanged)comboBox.Items).CollectionChanged +=
        ComboBoxItemsChanged;
}

But I cant seem to figure out how to detach it. Or is that handled automatically when the entire control is garbage collected?

Mizipzor
  • 51,151
  • 22
  • 97
  • 138
  • It is perhaps not a bad question, but the intent is dubious. What are you trying to do that you can't do through bindings and/or triggers? – Jay Aug 25 '10 at 10:07
  • @Jay: I want the ComboBox to autoselect the first value when the ItemsSource property is set if, and only if, Items.Count == 1 && SelectedItem == null; – Mizipzor Aug 25 '10 at 10:22
  • Are you using MVVM or any other presentation pattern? – Jay Aug 25 '10 at 10:29

1 Answers1

0

Implement IDisposable interface and detach event handler in Dispose method

RockWorld
  • 1,278
  • 2
  • 11
  • 24