根据事件XAML构建的应用程序更改Label的背景颜色

时间:2013-09-02 08:52:43

标签: c# wpf xaml .net-4.0

我正在构建一个检测事件的应用程序,在该事件中,指示事件被触发的Label应该改变颜色。

我正在使用XAML构建我的应用程序的界面,给标签初始的黄色很容易。双击时我希望它变为绿色。

    <!-- setting style for the identification labels -->
<Window.Resources>
    <Style x:Key="StatusLabelStyle" TargetType="Label">
        <Setter Property="Background" Value="Yellow" />
        <Setter Property="BorderBrush" Value="Yellow" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    </Style>
</Window.Resources>



                <Label Name="L1" Margin="10,50,10,0" Content="TEST A" Width="100" Height="60"
                       Style="{StaticResource StatusLabelStyle}" MouseDoubleClick="L1_MouseDoubleClick" />

我尝试通过代码隐藏文件执行此操作但反复失败。

1 个答案:

答案 0 :(得分:0)

您应该使用StoryboardAnimations

<Label.Triggers>
    <EventTrigger RoutedEvent="Label.MouseDoubleClick">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation  To="Green" Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Color)" FillBehavior="Stop" Duration="0.0.1"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger> 
</Label.Triggers>