问题描述
我有一个按钮,它应该是一个包含多个子项的菜单.菜单应该向下滑动"或类似的东西.我正在尝试使用简单的 TranslateTransform 来完成此操作,但我不断收到以下运行时错误:System.InvalidOperationException:在System.Windows.Controls.ContextMenu"的名称范围内找不到translateTransform"名称.代码>
I have a button, which should a menu with several subitems. The menu should "slide down" or anything like that. I am trying to accomplish this using simple TranslateTransform but I am constantly getting the following runtime error: System.InvalidOperationException: 'translateTransform' name cannot be found in the name scope of 'System.Windows.Controls.ContextMenu'.
是什么原因?有没有办法解决这个问题?是否有菜单动画方法的(免费)替代方法?
What is the cause? Is there a way to fix that? Are there any (free) alternatives to the menu animation approach?
<Button Name="Settings" Margin="2" Style="{StaticResource GrayGrayButtonStyle}">
<Image Source="gear.png" />
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Item 0"/>
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
<MenuItem Header="Item 3"/>
<ContextMenu.RenderTransform>
<TranslateTransform x:Name="translateTransform"
X="{Binding
Path=ActualWidth,
RelativeSource={RelativeSource
FindAncestor,
AncestorType={x:Type Button}}}" />
</ContextMenu.RenderTransform>
<ContextMenu.Triggers>
<EventTrigger RoutedEvent="ContextMenu.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="translateTransform"
Storyboard.TargetProperty="(TranslateTransform.X)"
To="0"
BeginTime="0:0:3.5"
AutoReverse="False"
Duration="0:0:2.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ContextMenu.Triggers>
</ContextMenu>
</Button.ContextMenu>
</Button>
推荐答案
不知道你要用那个动画来实现什么,但你可以这样写:
Don't know what exactly you're going to achieve with that animation, but you could write it like this:
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)"
To="0" ... />
不幸的是,什么都不会被动画化!TranslateTransform.X
属性已经为 0,因为它的绑定会导致错误.但是,您可以像这样编写 TranslateTransform 声明:
Unfortunatly nothing will be animated! The TranslateTransform.X
property is already 0, since its binding results in an error. You could however write your TranslateTransform declaration like this:
<TranslateTransform
X="{Binding Path=PlacementTarget.ActualWidth,
RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
这篇关于动画简单的 ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!