C# WPF:在鼠标悬停时为派生控件内容设置动画?
我有一个从按钮派生的控件:C# WPF:在鼠标悬停时为派生控件内容设置动画?,c#,wpf,animation,C#,Wpf,Animation,我有一个从按钮派生的控件: public class ToolButton : Button { static ToolButton() {} public ImageSource ImageSource { get { return (ImageSource)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } }
public class ToolButton : Button
{
static ToolButton() {}
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToolButton), new UIPropertyMetadata(null));
}
以及它的风格:
<Style TargetType="{x:Type wordplay:ToolButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type wordplay:ToolButton}">
<StackPanel Height="Auto" Orientation="Horizontal" Margin="4">
<Image Source="{TemplateBinding ImageSource}" Stretch="UniformToFill" />
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Left" Foreground="{DynamicResource TaskButtonTextBrush}" FontWeight="Bold" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,我需要在鼠标悬停时设置TextBlock width的动画,这样当用户将鼠标悬停在只显示图像的按钮上时,就会显示文本。如果用户把鼠标指针拿走,文本就会隐藏起来。我希望这是设置的风格,所以它将工作的任何工具按钮。问题是-我不知道ToolButton的最终大小,因此无法设置宽度动画。如何实现这一点?为什么需要设置宽度动画?如果你需要把它隐藏起来,那么为什么不玩可见性呢?当按钮没有悬停时,只有一个图像:;