WPF:如何在单击另一个按钮时为一个按钮设置动画?
我有以下 XAML 代码:
<Button Content="Menu 1" Height="23" Name="button1" Width="75" Margin="10">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.(TranslateTransform.Y)" From="0" To="200" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.RenderTransform>
<TranslateTransform X="0" Y="0" />
</Button.RenderTransform>
</Button>
当鼠标悬停在按钮上时,此代码会沿 Y 方向移动按钮。 但是,当我将鼠标移到另一个按钮上时,我想要相同的功能。 简而言之,假设我有 Button1 和 Button2。当我将鼠标移到 Button1 上时,Button2 应沿 Y 方向向下移动。我怎样才能做到这一点。任何人都可以提供相同的有效 XAML 代码吗?
I have the following XAML code:
<Button Content="Menu 1" Height="23" Name="button1" Width="75" Margin="10">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.(TranslateTransform.Y)" From="0" To="200" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.RenderTransform>
<TranslateTransform X="0" Y="0" />
</Button.RenderTransform>
</Button>
This code moves the button in the Y direction when the mouse is over the button.
However, I want the same functionality when I move my mouse over another button.
In short, let's assume I have Button1 and Button2. When I move my mouse over Button1, the Button2 should move down in the Y direction. How can I achieve this. Could anyone provide a working XAML code for the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以子类化 Button 并添加名为 ButtonToAnimate 的属性。
在事件触发器中,将 DoubleAnimation 的 Target 指向 ButtonToAnimate。
You could subclass Button and add a property named ButtonToAnimate.
In the event trigger, point the Target of the DoubleAnimation to the ButtonToAnimate.