如何在不设置名称的情况下设置动画WPF边框

时间:2017-01-17 10:27:46

标签: c# wpf animation border

我有这个:

<Border.Triggers>
    <EventTrigger RoutedEvent="Border.MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard TargetProperty="Background">
                    <ColorAnimation From="Red" To="Green" Duration="0:0:2" AutoReverse="True" RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Border.Triggers>

我想用鼠标悬停时改变背景,比如gradientstop或简单的颜色,但我得到错误。在哪里查找我必须在TargetProperty使用的依赖属性。

例如,这不起作用

<Grid.Triggers>
                            <EventTrigger RoutedEvent="Grid.Loaded">
                                <EventTrigger.Actions>
                                    <BeginStoryboard>
                                        <Storyboard TargetProperty="Background.GradientStops[1].Color">
                                            <ColorAnimation From="Red"
                                                            To="Green"
                                                            Duration="0:0:2"
                                                            AutoReverse="True"
                                                            RepeatBehavior="Forever" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                    <BeginStoryboard>
                                        <Storyboard TargetProperty="Background.GradientStops[1].Offset">
                                            <DoubleAnimation From="0"
                                                             To="1"
                                                             Duration="0:0:2"
                                                             AutoReverse="True"
                                                             RepeatBehavior="Forever"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>
                        </Grid.Triggers>

1 个答案:

答案 0 :(得分:0)

您应该将Border的Background属性设置为要设置动画的Brush。如果没有画笔,则没有任何动画效果。以下标记有效。它动画设置为边框背景的SolidColorBrush的Color属性:

DirectoryIterator
  

例如,这不起作用

这里也是一样的。如果要为GradientStop的Color属性设置动画,则需要确保实际存在GradientStop以进行动画处理,即应将Grid的Background属性设置为LinearGradientBrush。这有效:

<Border Background="Transparent">
    <Border.Triggers>
        <EventTrigger RoutedEvent="Border.MouseEnter">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard TargetProperty="(Border.Background).(SolidColorBrush.Color)">
                        <ColorAnimation From="Red" To="Green" Duration="0:0:2" AutoReverse="True" RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Border.Triggers>
    <TextBlock>Border....</TextBlock>
</Border>