【问题标题】:How to prevent the ribbon's application menu from opening outside the window?如何防止功能区的应用程序菜单在窗口外打开?
【发布时间】:2015-09-23 14:22:26
【问题描述】:

我们在程序中遇到了应用程序菜单的问题,即应用程序菜单在一个意外的位置打开,具体取决于窗口所在的位置。

最初,应用程序菜单会直接在功能区应用程序菜单按钮的下方和左侧打开。我们实现了this question 中概述的解决方案,这导致应用程序菜单以我们想要的方向(向下和向右)打开。

不幸的是,有一个副作用。如上所述,当窗口位于显示器边缘附近时,应用程序菜单会在一个意想不到的位置打开,尽管具体情况因屏幕配置而异:

  • 在单显示器系统上,当窗口与屏幕右边缘对齐时会出现这种情况。
  • 在多显示器系统上,当窗口位于除最左侧之外的任何屏幕的左边缘 20 像素范围内时,就会发生这种情况。

在任何一种情况下,应用程序菜单都会在应用程序窗口的左侧打开,与窗口至少相隔 50 像素。

以下是展示上述行为的精简应用程序。

主窗口:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
        xmlns:Example="clr-namespace:Ribbon_Example"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        x:Class="Ribbon_Example.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
      <Ribbon>         
         <Ribbon.ApplicationMenu>
            <RibbonApplicationMenu>
               <RibbonApplicationMenu.Resources>
                  <Example:NegativeIntegerConverter x:Key="NegativeIntegerConverter" />

                  <Style TargetType="Popup">
                     <Setter Property="Placement" Value="Left" />
                     <Setter Property="HorizontalOffset"
                             Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=RibbonApplicationMenu},
                                             Path=Width,
                                             Converter={StaticResource ResourceKey=NegativeIntegerConverter}}" />
                  </Style>
               </RibbonApplicationMenu.Resources>

               <RibbonApplicationMenuItem Header="New..." />
               <RibbonApplicationMenuItem Header="Open..." />
               <RibbonApplicationMenuItem Header="Close" />
               <RibbonApplicationMenuItem Header="Save" />
               <RibbonApplicationMenuItem Header="Save As..." />               
            </RibbonApplicationMenu>
         </Ribbon.ApplicationMenu>
      </Ribbon>
    </Grid>
</Window>

上述问题的类型转换器:

using System;
using System.Globalization;
using System.Windows.Data;

namespace Ribbon_Example
{
    class NegativeIntegerConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return -1 * System.Convert.ToInt32( value );
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return -1 * System.Convert.ToInt32( value );
        }
    }
}

一张图片胜过千言万语,所以这里是这个问题的多显示器版本的一个例子。

我们可能做错了什么?

【问题讨论】:

    标签: c# wpf ribbon


    【解决方案1】:

    更新 在我看来是这样的:

    我已经运行了您的代码,只是更改了您的代码

                      <Style TargetType="Popup">
                         <Setter Property="Placement" Value="Left" />
                         <Setter Property="HorizontalOffset"
                                 Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=RibbonApplicationMenu},
                                                 Path=Width,
                                                 Converter={StaticResource ResourceKey=NegativeIntegerConverter}}" />
                      </Style>
    

      <Style TargetType="Popup">
            <Setter Property="Placement" Value="Relative" />
      </Style>
    

    为我修复了您所展示的行为。我也在双显示器PC上运行

    【讨论】:

    • 不幸的是,此更改会导致应用程序菜单在正常情况下(靠近屏幕中心点时)向左打开(在窗口框架之外)。当位于屏幕的左边缘时,应用程序菜单仍会在窗口的左侧打开,但与该屏幕的边缘左对齐。这是我们遇到的原始行为。这对你来说是怎样的?
    • @Amelamise 你能测试一下添加&lt;Setter Property="FlowDirection" Value="LeftToRight"/&gt; 是否有帮助吗?
    • 这很奇怪。查看您的两个屏幕截图,我们的代码和引用是相同的,但行为完全不同。
    • 我刚刚尝试了 FlowDirection 属性 - 我相信它指的是语言的流向,而不是菜单的流向。无论哪种情况,菜单仍然向左打开。
    • @Amelamise 不正确的行为实际上是正确的。当您将Placement 设置为Left 时,如果有足够的空间,弹出窗口将尝试出现在父控件的左侧。转换器没有修复它,它只是将弹出 x 坐标移动到父级的宽度。通过将Placement 设置为BottomRelativeRight,您的弹出窗口应该可以正常工作。你能检查一下是不是这样吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 2012-01-08
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多