看來,當ContextMenu
加載ScrollContentPresenter
的菜單是不正確的尺寸,裁剪的MenuItem
S的ItemPresenter
(下面是一個顯示問題的可視化樹的刪節版)。
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
無效的的ContextMenu
的視覺根的度量(在PopupRoot
)當菜單被加載應引起的佈局被更新以顯示該ItemsPresenter
正確界限。
用於菜單的Load事件處理程序:
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方法:
private DependencyObject GetVisualTreeRoot(DependencyObject control)
{
DependencyObject parent = VisualTreeHelper.GetParent(control);
if (parent != null)
{
return GetVisualTreeRoot(parent);
}
else
{
return control;
}
}
沒人?請,我需要幫助:) –
好奇的行爲......它真的發生在你說的。對我來說好奇的是,如果在Visual Studio中運行,它發生的最多。當VS沒有運行時,它仍然會發生。對我來說,這是Connect的一個很好的候選人。 – NestorArturo
無法重現。可能是因爲我安裝了.Net 4.5? – Phil