There is a certain container. (In my case, this is InkCanvas ) He accordingly has a collection of child elements ( InkCanvas.Children ).

And this collection is changed in the code that I do not control (control is defined in my dll, and adding / deleting elements occurs in the code that uses my dll)

Question: Is it possible to receive notifications about the change of the collection? If so, how?

  • And why do you follow the control? The logic should be in the VM, not View. - VladD
  • In order to change the number of children to produce a certain processing and recounting of all current elements of the collection. I would be happy (and in part it is) to implement the logic of adding to the VM with the appropriate actions. But the canvas in edit mode supports hotkeys to copy / delete items and in no way notifies them about their use ... that’s just the problem .... The canvas in this case is chosen as an almost ready-to-use visual editor in order to save development time . But apparently you have to cut your version of the editor from scratch. - Alexey
  • Hmm, this is meanness on the part of InkCanvas, of course. - VladD
  • Wait, what about this: stackoverflow.com/a/729052/276994 ? - VladD
  • Yes, there was already an option to bind to the collection of elements. But InkCanvas does not allow to bind data to Child in the code. - Alexey

2 answers 2

Ok, here's another option.

If your internal elements are known in advance, subscribe to the Unloaded event with them:

 foreach (FrameworkElement child in IC.Children) child.Unloaded += (sender, args) => OnChildRemoved(child); // ... void OnChildRemoved(FrameworkElement child) { Console.WriteLine("Removed"); } 

If you add a child element manually, at this point you can subscribe to its Unloaded .

    Check Children for the implementation of INotifyCollectionChanged and cling to the CollectionChanged event if it implements.

    This is the default behavior for WPF.

    The minimal implementation will probably look something like this:

     private IEnumerable children; public IEnumerable Children { get { return children; } set { var notified = children as INotifyCollectionChanged; if (notified != null) notified.CollectionChanged -= NotifiedOnCollectionChanged; children = value; notified = children as INotifyCollectionChanged; if (notified != null) notified.CollectionChanged += NotifiedOnCollectionChanged; } } private void NotifiedOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { throw new NotImplementedException(); } 
    • The fact is that almost all standard WPF control containers have child elements stored in a UIElementCollection and it does not implement INotifyCollectionChanged. If this collection of elements had at least some event realized that something had changed - the question I asked here would not have arisen .... - Alexey
    • @Alexey, well, then such a solution is possible, it completely closes your case - social.msdn.microsoft.com/Forums/vstudio/en-US ...
    • Creating a class inheritor from InkCanvas that implements the behavior I need is also not an option. - Alexey