I have an ObservableCollection and I want to know when a control is added or removed from it. I have searched online and tried all of these questions and other websites but these did not help me at all!
Implementing CollectionChanged
Fire an event when Collection Changed (add or remove)
Here is my current code:
public partial class PageView : UserControl
{
public class PageViewCollection : ObservableCollection<PageViewPage>
{
public PageViewPage GetByName()
{
PageViewPage pgp = new PageViewPage();
foreach (PageViewPage page in this)
{
if (page.Name == ID)
{
pgp = page;
}
}
return pgp;
}
}
private PageViewCollection pages { get; set; }
[Bindable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
[Category("Behavior")]
public PageViewCollection Pages
{
get { return pages; }
set
{
if (pages != value)
{
pages = value; this.Invalidate();
if (PagesChanged != null)
{
PagesChanged(this, EventArgs.Empty);
}
}
}
}
[Browsable(true)]
public event EventHandler PagesChanged;
}
If I add a control, it still won't notify that the collection has changed. Any help is appreciated.