开发者

How to prevent animating menus in WPF?

Our designer is going nuts about this and I just cannot find the right search keywords to figure out how to fix it.

Menus and ContextMenus in WPF have "reveal" animations associated with them. We want to eliminate those without messing with system settings. I managed to pull the te开发者_JAVA百科mplate out using Blend but there's nothing in there about animations. Must be inheriting it from somewhere?

Can someone help? Any ideas would be appreciated.


In short, decyclone is right; it's coming from the OS, but here's why:

The default template for a MenuItem contains the markup and logic for displaying submenus. Look there.

When I opened a copy of a MenuItem template I found no fewer than four ControlTemplates, three Styles, two Brushes, the list went on. Use Blend to "Edit a Copy" of any MenuItem.

Inside the ControlTemplate relevant to a MenuItem's sub-menus is a control primitive called a Popup, declared like this:

            <Popup x:Name="PART_Popup" 
                   AllowsTransparency="true" 
                   Focusable="false" 
                   HorizontalOffset="-2" 
                   IsOpen="{Binding IsSubmenuOpen, 
                            RelativeSource={RelativeSource TemplatedParent}}" 
                   PopupAnimation="{DynamicResource 
                                    {x:Static SystemParameters.MenuPopupAnimationKey}}" 
                   Placement="Right" 
                   VerticalOffset="-3">

Note the PopupAnimation property; it points to SystemParameters.MenuPopupAnimationKey. That's the thing that's animating your menu.

This gives you two choices that occur to me, both of which will require that you define a custom template for the MenuItems in your app:

  • Re-configure PART_Popup so that AllowsTranparency="False"; or
  • Re-template MenuItem for your application, removing the PopupAnimation's reference to the OS animation.

You could then author your own triggers for animating how that popup appears.


A Context Menu or a Menu in any Windows Application is managed by Windows Operating System itself. You can go to Display Properties > Appearance to change how a Menu is displayed in the System.

For a WPF Application, you may able to subscribe to the Menu's Opening/Opened event and start a Storyboard to animate it. I am not sure on how to do that but I think that is the way you should be able to change the behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜