0

In my window, I have a UserControl that is dynamically created. I subscribe to two events of this UserControl: Loaded and MouseMove. In Loaded I create an AdornerLayer from UserControl, and in MouseMove I use this layer to add and remove particular adorners. But strangely, MouseMove event fires before Loaded and because of it my _adornerLayer instance is null, since it is not set yet.

Why is it happening? What is the right sequence of events to get _adornerLayer ready before any mouse events?

Dmitry Arestov
  • 1,427
  • 12
  • 24
  • The Loaded-Event is fired when: "The Loaded event says that the tree is not only built and initialized, but layout has run on it, data has been bound, it's connected to a rendering surface (window), and you're on the verge of being rendered." (Source: http://blogs.msdn.com/b/mikehillberg/archive/2006/09/19/loadedvsinitialized.aspx). A possible solution could be to subscribe to the "MouseMove" Event in a method handling the "Loaded" Event. Also why do you create your AdornerLayer in the Loaded Event and not e.g. in the Constructor of the UserControl? – SSchuette Dec 05 '14 at 07:14
  • Because it is impossible to create AdornerLayer on a control that is not fully loaded (or shown). I used to call `AdornerLayer.GetAdornerLayer` straight after UserControl creation, but it returned null, so I had to postpone this call to Loaded event. – Dmitry Arestov Dec 05 '14 at 07:21

0 Answers0