我想在WPF中以編程方式創建按鈕,當IsMouseOver == true
增長到特定大小時,在IsMouseOver == false
時縮小到原始大小。如何指定一個DependencyProperty作爲資源
爲了實現這一行爲,我得到的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
與ControlTemplate
,使我的按鍵的自定義外觀,並添加了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
作爲一種資源?
或者是否有其他方式實現可縮放按鈕?
我不明白你的問題......這段代碼有什麼問題? –
'originalScale'與1.0有什麼不同嗎?你的'MouseOverAnimation'如何考慮? – Clemens
爲什麼從'Button'完全得到?爲什麼不直接在自定義模板中做所有這些? –