如何修改WPF功能区应用程序菜单下拉位置



是否可以修改10/2010 WPF功能区的应用程序菜单的下拉位置?我认为菜单在最左边的位置打开是很不寻常的,所以我想改变它。

示例:在Word 2007(你可能都知道,它有旧的ribbon设计)中,应用程序菜单尽可能地向右打开。我也想获得这种行为,因为在右边的是菜单唯一合理的位置。它的所有条目都在左列,在按钮的正下方。我没有找到任何简单的方法来指定它的左位置。有人知道这是可能的吗?

好吧,经过几个小时的尝试和错误,我找到了一个可行的方法。它不像"Windows 7 Paint"或类似的Windows 7 ribbon应用程序那样100%的原始行为,但在大多数情况下它是有效的。

首先,您需要知道应用程序菜单是用Popup实现的,它具有Placement属性来定义弹出窗口打开的位置。您需要覆盖PlacementMode.Left的默认行为。这将使弹出式菜单在菜单按钮旁边打开。

接下来,您需要将Popup.HorizontalOffset属性设置为否定的RibbonApplicationMenu.Width。这是通过绑定和转换器来实现的。

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

转换器在RibbonWindow.Resources中定义如下:

<r:RibbonWindow.Resources>
    <local:NegateIntegerConverter x:Key="NegateIntegerConverter"/>
</r:RibbonWindow.Resources>

local命名空间必须在RibbonWindow:

内部声明。
<r:RibbonWindow x:Class="MainWindow"
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    xmlns:local="clr-namespace:ApplicationRootNamespace"
>
最后,NegateIntegerConverter的代码是应用程序根名称空间中的一个类:
Public Class NegateIntegerConverter
  Implements IValueConverter
  Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Return -CInt(value)
  End Function
  Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    Return -CInt(value)
  End Function
End Class
Class MainWindow
End Class

现在是行为的不同:如果菜单不能完全向右展开,因为屏幕在那里结束,弹出窗口不是简单地向左转一点,而是完全在左边。也许我可以找出它实际上是如何像"Windows 7 Paint"功能区菜单一样的行为,但在此之前这是一个很好的解决方案。

最新更新