我是WPF动画的新手。
我有一个带有4个按钮的WPF应用程序。这4个按钮具有动画,其中按钮的不透明度从0.0变为1.0,当鼠标悬停在该按钮上时会触发该不透明度。
我面临的问题是,即使鼠标滑过其他按钮几秒钟,这些按钮的动画也会被触发。
只有当鼠标停留在按钮上至少一秒钟时,我才能触发动画吗?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="22.5*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="22.5*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="22.5*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="22.5*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Button x:Name="Button1" Grid.Column="1" Content="Button 1">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.0" To="1.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Button1" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="Button2" Grid.Column="3" Content="Button2">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.0" To="1.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Button2" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="Button3" Grid.Column="5" Content="Button 3">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.0" To="1.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Button3" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="Button4" Grid.Column="7" Content="Button 4">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.0" To="1.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Button4" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
提前致谢
最佳答案
我认为这不是您可以在XAML中执行的操作。在代码中,它看起来像这样:
XAML
<Button Width="50" Height="50" Name="MyButton"> </Button>
码
public partial class MainWindow : Window
{
private DispatcherTimer timer;
private DoubleAnimation animation;
private Storyboard sb;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
MyButton.MouseEnter += (sender, e) =>
{
timer.Start();
};
MyButton.MouseLeave += (sender, e) =>
{
timer.Stop();
};
animation = new DoubleAnimation();
animation.From = 1;
animation.To = 0;
animation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
Storyboard.SetTarget(animation, MyButton);
Storyboard.SetTargetProperty( animation, new PropertyPath(OpacityProperty));
sb = new Storyboard();
sb.Children.Add(animation);
}
private void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
sb.Begin();
}
}
关于c# - 仅当鼠标悬停很长时间时才如何触发wpf动画?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27537305/