如何设置控件部分的动画';WPF中的模板是什么?
因此,为了这个例子,我简化了它。我有一个按钮,在它的控制模板中有一个红色椭圆。单击按钮时(MouseDown事件),我希望椭圆动画在1秒后变为透明。这是我的xaml如何设置控件部分的动画';WPF中的模板是什么?,wpf,xaml,wpf-controls,wpf-animation,Wpf,Xaml,Wpf Controls,Wpf Animation,因此,为了这个例子,我简化了它。我有一个按钮,在它的控制模板中有一个红色椭圆。单击按钮时(MouseDown事件),我希望椭圆动画在1秒后变为透明。这是我的xaml <Button> <Button.Template> <ControlTemplate> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
<Button>
<Button.Template>
<ControlTemplate>
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
ClipToBounds="True">
<Ellipse x:Name="ripple"
Height="20" Width="20"
Fill="Red"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Opacity="1"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Button.Template>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ripple"
Storyboard.TargetProperty="Opacity"
From="1" To="0" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
问题是当我点击按钮时,椭圆不是透明的。我的猜测是,出于某种原因,它找不到名为“ripple”的对象,但我不知道为什么。我做错了什么?将
EventTrigger
移动到ControlTemplate.Triggers
并使用PreviewMouseDown
事件,因为按钮处理MouseDown
事件
<Button>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
ClipToBounds="True">
<Ellipse
x:Name="ripple"
Height="20"
Width="20"
Fill="Red"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Opacity="1"/>
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ripple"
Storyboard.TargetProperty="Opacity"
From="1"
To="0"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>