It seems that when the ContextMenu
is loaded the ScrollContentPresenter
for the menu isn't sized correctly, clipping the ItemPresenter
of the MenuItem
s (Below is an abridged version of the visual tree showing the issue).
PopupRoot, Acutal Width: 219,027, Desired Width: 219,027
Decorator, Acutal Width: 219,027, Desired Width: 219,027
NonLogicalAdornerDecorator, Acutal Width: 219,027, Desired Width: 219,027
ContextMenuProxy, Acutal Width: 219,027, Desired Width: 219,027
SystemDropShadowChrome, Acutal Width: 214,027, Desired Width: 219,027
Border, Acutal Width: 214,027, Desired Width: 214,027
Grid, Acutal Width: 212,027, Desired Width: 212,027
Rectangle, Acutal Width: 28,000, Desired Width: 32,000
Rectangle, Acutal Width: 1,000, Desired Width: 31,000
Rectangle, Acutal Width: 1,000, Desired Width: 32,000
ScrollViewer, Acutal Width: 210,027, Desired Width: 212,027
Grid, Acutal Width: 210,027, Desired Width: 210,027
Border, Acutal Width: 210,027, Desired Width: 210,027
ScrollContentPresenter, Acutal Width: 210,027, Desired Width: 210,027
ItemsPresenter, Acutal Width: 241,047, Desired Width: 245,047
Invalidating the measure of the of the ContextMenu
's visual root (the PopupRoot
) when the menu is loaded should cause the layout to be updated to display the correct bounds for the ItemsPresenter
.
The handler for the menu's Load event:
private void mainMenu_Loaded(object sender, RoutedEventArgs e)
{
if (sender != null)
{
ContextMenu menu = sender as ContextMenu;
if (menu != null)
{
// get the visual root for the context menu
var root = (FrameworkElement)GetVisualTreeRoot(menu);
// invalidate the menu's layout
root.InvalidateMeasure();
}
}
}
GetVisualTreeRoot method:
private DependencyObject GetVisualTreeRoot(DependencyObject control)
{
DependencyObject parent = VisualTreeHelper.GetParent(control);
if (parent != null)
{
return GetVisualTreeRoot(parent);
}
else
{
return control;
}
}