Windows 10上ContextMenu和Popup WPF控件的对齐错误 [英] Wrong alignment of ContextMenu and Popup WPF controls on Windows 10

查看:173
本文介绍了Windows 10上ContextMenu和Popup WPF控件的对齐错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常奇怪的问题,我希望有人知道如何解决. 情况:

It's really strange problem which I hope somebody knows how to solve. Situation:

  1. 我们的WPF项目具有一个窗体,在该窗体上我们可以显示上下文菜单(ContextMenu控件)和弹出窗口(Popup控件),以响应某些用户操作.在大多数情况下,它是在鼠标单击某些文本块时发生的. 这是一个示例:

  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;

  • 弹出窗口必须与其放置目标的左上角对齐(在我们的示例中为某些文本块),因此-使其重叠.而且,除了具有平板电脑模式的Windows 10机器(例如MS Surface Pro)以外,它在所有情况下均能正常工作. 在这种环境下,我们的弹出窗口会出现在放置目标的左侧,因此-请勿重叠.

  • 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

    As we discovered it happens because of the special Windows 10 option described in this article: http://www.isunshare.com/windows-10/show-context-menu-on-left-or-right-in-windows-10.html

    默认情况下,此选项设置为右手"(这是可以理解的),但我不明白为什么它会以这种方式影响Popup控件,甚至在桌面模式下也会发生.

    This option is set to "Right-handed" by default (which is understandable) but I don't understand why it influences in such way on Popup control and why it happens even in Desktop mode.

    找到一种方法来更改此默认行为,并使我们的Popup在任何情况下都与父控件的左上角对齐,将是很棒的选择. 有人知道怎么做吗?

    It would be great to find a way to change this default behavior and make our Popup appear aligned to top-left corner of parent control in any case. Does anybody know how to do it?

    推荐答案

    您可以指定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>
    

    即使在使用惯用右手平板电脑模式的情况下,该外观也是如此:

    Which looks like this, even with right-handed tablet mode engaged:

    这里的问题是Y属性设置为正确的高度.如果您有固定高度的弹出窗口,那可能很好.否则,您可以尝试使用内容的ActualHeight,但是由于Rect.Y不是依赖项属性,因此您需要从doubleRect的转换器.

    The hack here is that the Y property is set to the correct height. If you have a fixed height popup, that might be fine. Otherwise you could try bing to the content's ActualHeight, but as Rect.Y is not a dependency property you'd need a converter from double to Rect for that purpose.

    这篇关于Windows 10上ContextMenu和Popup WPF控件的对齐错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆