上下文菜单被切断在WPF某些情况下, [英] Context menu is cut in some situations in WPF
问题描述
右键菜单被截断在不同的.NET框架。看到里面的ZIP文件的图像(有两个屏幕,一个来自XP等也从Win7)。
Context menu is truncated in different .NET Framework. See images inside ZIP file (there are two screenshots, one from XP and other from Win7).
我创建了一个简单的Visual Studio 2010的解决方案,瑞普我的问题。
I created a simple Visual Studio 2010 solution which repro my issue.
( http://www.mediafire.com/download.php?doq7gsh75qgvzwq )。
在XP似乎做工精细,但Windows 7。
On XP it seems to work fine, but not on Windows 7.
这个问题可以复制在Windows 7中,如果目标的.NET Framework 3.5(包括SP1)(请参阅从压缩图像)。
The issue can be reproduced on Windows 7 if target .NET Framework is 3.5 (including SP1) (please see the image from zip).
如果我更改目标框架4.0,它工作正常还对Windows 7操作系统。
If I change the target framework to 4.0 it works fine also on Windows 7.
时的一个解决方案,使上下文菜单完全可见的.NET Framework 3.5的Windows 7操作系统吗?
Is a solution to make context menu full visible in .NET Framework 3.5 on Windows 7 OS ?
推荐答案
看来,文本菜单
加载时的 ScrollContent presenter
的菜单是不正确的尺寸,裁剪项目presenter
的菜单项$ C $的C> S(下面是可视化树显示问题的删节版)。
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
无效化的的文本菜单
的视觉根的措施(即 PopupRoot
)菜单时加载应引起布局进行更新以显示为产品presenter正确的边界
。
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
.
的处理程序菜单中的Load事件:
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方式:
GetVisualTreeRoot method:
private DependencyObject GetVisualTreeRoot(DependencyObject control)
{
DependencyObject parent = VisualTreeHelper.GetParent(control);
if (parent != null)
{
return GetVisualTreeRoot(parent);
}
else
{
return control;
}
}
这篇关于上下文菜单被切断在WPF某些情况下,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!