请检查下面的代码是否适用于您。对标签内容属性的任何更改都将设置动画。它是通过使用触发器和值转换器类来完成的,这个类将内容值转换为“真”或“假”,并设置触发器对这两个值做出反应。cnverter附加到标签的tag属性,该属性被弯折为数据上下文的name属性。另外,我还添加了一些动画到鼠标输入和离开事件中,这些事件非常简单,只使用XAML中的RouteRevents完成。
转换器:
public class TestConverter : IValueConverter
{
private string _originalValue = String.Empty;
private bool _previousValue = false;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
_originalValue = (string)value;
_previousValue = !_previousValue;
return _previousValue.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return _originalValue;
}
}
数据上下文初始化:
label1.DataContext = new Test() { Name = DateTime.Now.ToString() };
XAML:
<Window.Resources>
<local:TestConverter x:Key="TestConverter" />
</Window.Resources>
<Grid>
<Label
Height="28"
HorizontalAlignment="Left"
Margin="132,96,0,0"
Name="label1" VerticalAlignment="Top" Width="120">
<Label.Content>
<Binding Path="Name"/>
</Label.Content>
<Label.Tag>
<Binding Path="Name" Converter="{StaticResource TestConverter}"/>
</Label.Tag>
<Label.Background>
<SolidColorBrush x:Name="animatedBrush1" Color="Yellow" />
</Label.Background>
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard AutoReverse="True">
<!--<DoubleAnimation
Storyboard.TargetProperty="FontSize" To="20"/>-->
<DoubleAnimation
Storyboard.TargetProperty="Opacity" To="0.0" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="Tag" Value="False">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard AutoReverse="True">
<DoubleAnimation
Storyboard.TargetProperty="FontSize" To="20"/>
<!--"<DoubleAnimation
Storyboard.TargetProperty="Opacity" To="0.0" AutoReverse="True"/>-->
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
<Label.Triggers>
<EventTrigger RoutedEvent="Label.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="animatedBrush1"
Storyboard.TargetProperty="Color"
To="Blue" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Label.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="animatedBrush1"
Storyboard.TargetProperty="Color"
To="Yellow" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Label.Triggers>
</Label>
</Grid>
希望这有帮助,问候