/// <summary> /// Initialize the popup control /// It is smart enough to handle multiple calls /// </summary> private void InitializePopup() { if (popup == null) { // Add overlay which is the size of RootVisual overlay = new Canvas { Background = new SolidColorBrush(Colors.Transparent), Width = RootVisual.ActualWidth, Height = RootVisual.ActualHeight }; overlay.Children.Add(this); // Dismiss the context menu if clicked else where overlay.AddHandler(MouseLeftButtonUpEvent , new MouseButtonEventHandler(OnOverlayMouseButtonUp) , true); // Initialize popup to draw the context menu over all controls popup = new Popup { Child = overlay }; // NOTE: Tried to add to visual tree, // to make popup orientation aware, but did't work //afterTemplateApplied.Enqueue(() => // { // var panel = FindPanel(Owner); // if (panel != null) // { // panel.Children.Add(popup); // } // }); } }