Datatrigger上的按钮闪烁

时间:2013-08-02 05:47:31

标签: wpf xaml button storyboard datatrigger

我想在DataTrigger触发时有一个闪烁/动画的按钮。我想为按钮的背景设置动画。下面是我的xaml代码。

<Window.Resources>
    <Style x:Key="ButtonStyle"  TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Name="StartBlinking">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="False">
                <DataTrigger.EnterActions>
                    <RemoveStoryboard BeginStoryboardName="StartBlinking"/>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid>
    <Grid>
        <Button x:Name="Button" Content="Button" Width="25" Height="25" Margin="158,62,320,224" Click="Button_Click"></Button>
        <Button Style="{StaticResource ButtonStyle}" Content="Button" Focusable="False" Height="75" HorizontalAlignment="Left" Margin="23,146,0,0" Name="btnImgBrush" VerticalAlignment="Top" Width="160"></Button>

    </Grid>
</Grid>

以下是后端代码:

public Boolean Notification
    {
        get { return new_notification; }

        set 
        {
            new_notification = value;
            RaisePropertyChanged("Notification");
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (Notification)
        {
            Notification = false;
        }
        else
        {
            Notification = true;
        }
    }

但是,它没有用。任何想法为什么它不起作用?

非常感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:4)

最后它的工作。谢谢:))

<Window.Resources>
<Style x:Key="ButtonStyle"  TargetType="{x:Type Button}">
  <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="Transparent"/>
            </Setter.Value>
        </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard Name="StartBlinking">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="False">
            <DataTrigger.EnterActions>
                <RemoveStoryboard BeginStoryboardName="StartBlinking"/>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

答案 1 :(得分:0)

试试这个 -

<Style x:Key="ButtonStyle"  TargetType="{x:Type Button}">
 <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Notification,RelativeSource={RelativeSource AncestorType=Window}}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Name="StartBlinking">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="Transparent" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=Notification,,RelativeSource={RelativeSource AncestorType=Window}}" Value="False">
                            <DataTrigger.EnterActions>
                                <RemoveStoryboard BeginStoryboardName="StartBlinking"/>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
</Style>

答案 2 :(得分:0)

TM火箭 Vishal 答案都是正确的,但以下方法可能是最正确,最干净的方法

<Window.Resources>
  <Style x:Key="ButtonStyle"  TargetType="{x:Type Button}">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="Transparent"/>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Notification}" Value="True">
        <DataTrigger.EnterActions>
            <BeginStoryboard Name="StartBlinking">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="Orange" Duration="00:00:00.4" RepeatBehavior="Forever" AutoReverse="True"/>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.EnterActions>
        <DataTrigger.ExitActions>
            <RemoveStoryboard BeginStoryboardName="StartBlinking"/>
        </DataTrigger.ExitActions>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Window.Resources>