void PopupMenuItem_Loaded(object sender, RoutedEventArgs e) { _isLoaded = true; this.Loaded -= PopupMenuItem_Loaded; //this.SetValue(PopupMenuItem.IsVisibleProperty, IsVisible); // The IsVisible setter is only applicable in the Loaded event because the item container it targets is not accessible before that var container = PopupMenu.GetContainer <FrameworkElement>(this); container.Visibility = (bool)IsVisible ? Visibility.Visible : Visibility.Collapsed; if (UseFirstItemAsHeader && this.Items.Count > 0) { FrameworkElement element = this.Items.First() as FrameworkElement; this.Items.Remove(element); // Use first child as header if the latter is not assigned this.Header = element; this.UpdateLayout(); } }