如何修改WPF功能区应用程序菜单的下拉位置? [英] How can I modify the WPF ribbon application menu dropdown position?

查看:152
本文介绍了如何修改WPF功能区应用程序菜单的下拉位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以修改10/2010 WPF功能区的应用程序菜单的放置位置?我认为在最左侧打开菜单是非常不寻常的,所以我想更改它.

Is it possible to modify the application menu's drop position for the 10/2010 WPF ribbon? I think it's very unusual that a menu opens at the leftmost position, so I'd like to change that.

示例:在Word 2007中(大家都知道,它具有旧的功能区设计),应用程序菜单将尽可能向右打开.我也想得到这种行为,因为在右边是菜单上唯一明智的位置.它的所有条目都在左列中,该列位于该按钮的正下方.我没有找到任何简单的方法来分配其左侧位置.有人知道这是否可行以及如何实现?

Example: in Word 2007 (which - as you probable all know - has the old ribbon design) the application menu opens as far as possible to the right. I'd like to get this behaviour, too, because at the right is the only sensible position for the menu. All its entries are in the left column, which was right below the button then. I've not found any easy way to assign its left position. Does anybody know if and how this is possible?

推荐答案

好吧,经过数小时的尝试和错误,我找到了一种可行的方法.这不是"Windows 7 Paint"或类似的Windows 7功能区应用程序中100%的原始行为,但在大多数情况下都可以.

Okay, after many hours of try and error, I found a possible way to do it. It's not the 100% original behaviour like in "Windows 7 Paint" or similar Windows 7 ribbon applications, but in most cases it works.

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

First, you need to know that the application menu is realized with a Popup, which has a Placement property to define where the popup is opened. You need to override the default behaviour to PlacementMode.Left. This will make the popup menu open up right next to the menu button.

接下来,您需要将Popup.HorizontalOffset属性设置为否定的RibbonApplicationMenu.Width.这是通过绑定和转换器来取反值来完成的.

Next, you need to set the Popup.HorizontalOffset property to the negated RibbonApplicationMenu.Width. This is done via Binding and a converter to negate the value.

<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中定义的转换器如下:

The converter is defined in the RibbonWindow.Resources as follows:

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

必须在RibbonWindow内部声明local命名空间:

The localnamespace has to be declared inside of the RibbonWindow:

<r:RibbonWindow x:Class="MainWindow"
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    xmlns:local="clr-namespace:ApplicationRootNamespace"
>

最后,NegateIntegerConverter的代码是应用程序的根名称空间中的类:

And finally, the code for the NegateIntegerConverter is a class in the application's root namespace:

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"功能区的菜单一样,但这是一个很好的解决方法.

Now for the difference in behaviour: if the menu cannot fully expand to the right because the screen is ending there, the popup does not simply open up a bit farther to the left, but completely on the left. Maybe I can find out how it's actually behaving like the "Windows 7 Paint" ribbon's menu, but this is a good workaround until then.

这篇关于如何修改WPF功能区应用程序菜单的下拉位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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