我想在 WPF 中以编程方式创建按钮,该按钮在 何时增长到特定大小IsMouseOver == true
,并在 何时缩小到其原始大小IsMouseOver == false
。
为了实现这种行为,我派生了Button
该类,并添加了一个DependencyProperty
:
public class ScalableButton : Button
{
public Storyboard MouseOutAnimation
{
get { return (Storyboard)GetValue(MouseOutAnimationProperty); }
set { SetValue(MouseOutAnimationProperty, value); }
}
// Using a DependencyProperty as the backing store for MouseOutAnimation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOutAnimationProperty =
DependencyProperty.Register("MouseOutAnimation", typeof(Storyboard), typeof(ScalableButton), new UIPropertyMetadata(null));
public ScalableButton(double originalScale, Style style)
: base()
{
CreateMouseOutAnimation(originalScale);
RenderTransform = new ScaleTransform(originalScale, 1, 0.5, 0.5);
RenderTransformOrigin = new Point(0.5, 0.5);
Style = style;
ApplyTemplate();
}
private void CreateMouseOutAnimation(double originalScale)
{
DoubleAnimation animX = new DoubleAnimation();
animX.To = originalScale;
animX.Duration = TimeSpan.FromMilliseconds(200);
DoubleAnimation animY = new DoubleAnimation();
animY.To = 1;
animY.Duration = TimeSpan.FromMilliseconds(200);
Storyboard sb = new Storyboard();
sb.Children.Add(animX);
sb.Children.Add(animY);
Storyboard.SetTarget(sb, this.RenderTransform);
Storyboard.SetTargetProperty(animX, new PropertyPath(ScaleTransform.ScaleXProperty));
Storyboard.SetTargetProperty(animY, new PropertyPath(ScaleTransform.ScaleYProperty));
MouseOutAnimation = sb;
}
}
然后,我创建了一个Style
用 aControlTemplate
为我的按钮制作自定义外观,并添加了一个Storyboard
资源来放大它们,另一个来缩小它们,这就是问题所在:
<Style TargetType="{x:Type rgw:ScalableButton}" x:Key="EllipseWithText" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type rgw:ScalableButton}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOverAnimation">
<DoubleAnimation Storyboard.TargetName="ButtonGrid" Storyboard.TargetProperty="RenderTransform.ScaleX" To="1.2" Duration="0:0:1" />
<DoubleAnimation Storyboard.TargetName="ButtonGrid" Storyboard.TargetProperty="RenderTransform.ScaleY" To="1.2" Duration="0:0:1" />
</Storyboard>
<Storyboard x:Key="MouseOutAnimation">
<!-- This would be the one which scales down -->
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="ButtonGrid" RenderTransform="{TemplateBinding RenderTransform}" RenderTransformOrigin="0.5, 0.5">
<Ellipse x:Name="ButtonEllipse" Height="50" Width="150" Fill="#00000000" StrokeThickness="1" Stroke="Black" />
<TextBlock x:Name="ButtonText" Width="150" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
那么,如何将 指定ScalableButton.MouseOutAnimation
为资源?
或者有没有其他方法可以实现可扩展按钮?