WPF 中某些情况下上下文菜单被剪切

发布于 2024-12-20 07:59:53 字数 506 浏览 1 评论 0原文

上下文菜单在不同的 .NET Framework 中被截断。查看 ZIP 文件中的图像(有两张截图,一张来自 XP,另一张来自 Win7)。

我创建了一个简单的 Visual Studio 2010 解决方案来重现我的问题。

http://www.mediafire.com/download.php?doq7gsh75qgvzwq)。

在 XP 上似乎工作正常,但在 Windows 7 上不行。

如果目标 .NET Framework 是 3.5(包括 SP1),则可以在 Windows 7 上重现该问题(请参阅 zip 中的图像)。

如果我将目标框架更改为 4.0,它在 Windows 7 上也能正常工作。

是否有解决方案使上下文菜单在 Windows 7 操作系统上的 .NET Framework 3.5 中完全可见?

Context menu is truncated in different .NET Framework. See images inside ZIP file (there are two screenshots, one from XP and other from Win7).

I created a simple Visual Studio 2010 solution which repro my issue.

( http://www.mediafire.com/download.php?doq7gsh75qgvzwq ).

On XP it seems to work fine, but not on Windows 7.

The issue can be reproduced on Windows 7 if target .NET Framework is 3.5 (including SP1) (please see the image from zip).

If I change the target framework to 4.0 it works fine also on Windows 7.

Is a solution to make context menu full visible in .NET Framework 3.5 on Windows 7 OS ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻梦旅人 2024-12-27 07:59:53

似乎当加载 ContextMenu 时,菜单的 ScrollContentPresenter 大小不正确,从而剪切了 MenuItem 的 ItemPresenter /code>s(下面是显示问题的可视化树的删节版本)。

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) 的测量无效应该会导致布局更新以显示 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;
    }
}

It seems that when the ContextMenu is loaded the ScrollContentPresenter for the menu isn't sized correctly, clipping the ItemPresenter of the MenuItems (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;
    }
}
无法回应 2024-12-27 07:59:53

解决方法:

<ContextMenu x:Name="mainMenu" Width="300" >

设置固定宽度时似乎不再麻烦。
仍然是 Connect 的良好候选者。

A workaround:

<ContextMenu x:Name="mainMenu" Width="300" >

It seems to stop bothering when setting a fixed width.
Still a good candidate for Connect.

挽梦忆笙歌 2024-12-27 07:59:53

我也可以在 .Net 4.5.1 中重现此问题。
使用上面标记的解决方案也无法解决。 InvalidateMeasure 仍然会导致
有时上下文菜单为空,然后它开始出现。当我窥探上下文菜单时,
发现菜单 ItemsPanel 大小计算完成得很好,但 ScrollContentPresenter 大小为 0。任何人都面临类似的问题。
我的解决方法是:

    private static void ContextMenuOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var menu = (ContextMenu)sender;
        if (menu.HasItems)
        {
            menu.MinHeight = menu.Items.Count * 25;
        }

        menu.Loaded -= ContextMenuOnLoaded;
    }

不确定这是否是最佳解决方案。但为什么会发生这种情况也令人惊讶。

I am able to reproduce this issue in .Net 4.5.1 also.
Not able to solve using above marked solution as well. InvalidateMeasure still results in
empty context menu sometimes and it starts appearing. When I snoop the context menu,
found out that menu ItemsPanel size calculation is done fine, but ScrollContentPresenter size is 0.Anyone facing similar issues.
My workaround is :

    private static void ContextMenuOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var menu = (ContextMenu)sender;
        if (menu.HasItems)
        {
            menu.MinHeight = menu.Items.Count * 25;
        }

        menu.Loaded -= ContextMenuOnLoaded;
    }

Not sure if it is the best solution. But why does it happen is also surprising.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文