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

How to set Animation WPF Border without setting a name

我有这个:

<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>

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

<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>

for example this doesnt work either

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

<Grid>
    <Grid.Background>
        <LinearGradientBrush>
            <GradientStop />
            <GradientStop />
        </LinearGradientBrush>
    </Grid.Background>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Grid.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard TargetProperty="Background.GradientStops[0].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>
    <TextBlock>Grid...</TextBlock>
</Grid>