The context menu is cut in some situations in WPF

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

I created a simple Visual Studio 2010 solution that reproduces my problem.

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

On XP, this works fine, but not on Windows 7.

The problem can be reproduced in Windows 7 if the target .NET Framework 3.5 (including SP1) (see image from zip).

If I change the target structure to 4.0, it works fine on Windows 7 as well.

Is the decision to make the context menu fully visible in the .NET Framework 3.5 on Windows 7?

+4
source share
3 answers

It seems that when loading ContextMenu ScrollContentPresenter menu is not set to the correct value, trimming ItemPresenter MenuItem (Below is an abridged version of the visual tree showing the problem).

 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 

An invalid measure of the visual root of ContextMenu ( PopupRoot ) when loading the menu should result in a layout update to display the correct borders for the ItemsPresenter .

Handler for the Download Event menu:

 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 layout root.InvalidateMeasure(); } } } 

GetVisualTreeRoot Method:

 private DependencyObject GetVisualTreeRoot(DependencyObject control) { DependencyObject parent = VisualTreeHelper.GetParent(control); if (parent != null) { return GetVisualTreeRoot(parent); } else { return control; } } 
+4
source

Workaround:

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

It seems to stop worrying when setting a fixed width. Another good candidate for Connect.

+1
source

I can also reproduce this problem in .Net 4.5.1. Failed to solve using the above solution. InvalidateMeasure still leads to an empty context menu, and it starts to appear. When I look at the context menu, I find out that the ItemsPanel menu is calculated by size, but the ScrollContentPresenter size is 0. Anyone faced with similar problems. My workaround:

  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 this is the best solution. But why this happens is also amazing.

0
source

Source: https://habr.com/ru/post/1385073/


All Articles