在 Windows 10 上,ContextMenu 和 Popup WPF 控件对齐错误。

3
这是一个很奇怪的问题,我希望有人知道如何解决。 情况:
  1. Our WPF project has a form on which we show context menus (ContextMenu control) and popups (Popup control) in response on some user actions. In most cases it happens on mouse click on some text block. Here is an example:

            popup = new Popup {
                Placement = PlacementMode.Relative,
                PlacementTarget = textBlock,
                StaysOpen = false
            }; 
    
            .   .   .   .   .
            //later on mouse click:
            popup.IsOpen = true;
    
  2. The popup must appear aligned to the top-left corner of its placement target (some text block in our example) and so - overlap it. And it works just well in all cases except Windows 10 machines which have tablet mode (like MS Surface Pro). In such environments our popups appear to the left side of the placement targets and so - do not overlap them.

我们发现这是由于Windows 10中的特殊选项引起的,该选项在本文中有描述:http://www.isunshare.com/windows-10/show-context-menu-on-left-or-right-in-windows-10.html 默认情况下,此选项设置为“右手”,这是可以理解的,但我不明白为什么它会以这种方式影响弹出控件,甚至在桌面模式下也是如此。
找到一种改变此默认行为并使我们的弹出窗口在任何情况下都与父控件的左上角对齐的方法将是很好的。有人知道怎么做吗?

1
这里似乎有一个类似的问题: https://dev59.com/b2Ml5IYBdhLWcg3wzpnV - Sergiy
1个回答

1
你可以指定一个PlacementRectangle:
<Border Width="300" Height="20" Background="Yellow">
  <Popup IsOpen="True" Placement="Left">
      <Popup.PlacementRectangle>
        <Rect X="0" Y="20" Width="0" Height="0" />
      </Popup.PlacementRectangle>
    <Border Width="100" Height="100" Background="Blue"></Border>
  </Popup>
</Border>

即使启用了右手平板模式,它看起来也像这样:

enter image description here

这里的技巧在于将Y属性设置为正确的高度。如果您有一个固定高度的弹出窗口,那可能没问题。否则,您可以尝试使用内容的ActualHeight,但由于Rect.Y不是依赖属性,因此您需要一个从doubleRect的转换器来实现这个目的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接