边框OnMouseDown的WPF背景更改

时间:2011-01-26 20:58:43

标签: wpf storyboard eventtrigger coloranimation

我正在尝试更改鼠标左键上边框的背景。按钮我不明白。当然我的渐变有3站。

Eventtrigger:

<EventTrigger  RoutedEvent="UIElement.PreviewMouseLeftButtonDown" SourceName="border">
    <BeginStoryboard x:Name="MouseDown_BeginStoryboard" Storyboard="{StaticResource OnMouseDown}"/>
</EventTrigger>

故事板:

<Storyboard x:Key="OnMouseDown">
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[0].Color" To="Red" />
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[1].Color" To="Red" />
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[2].Color" To="Red" />
</Storyboard>

如果有人看到错误,请告诉我;)

1 个答案:

答案 0 :(得分:1)

我没有看到任何明显的错误,我使用您发布的代码创建的示例项目工作得很好。也许你可以用它来找到问题的根源

<Window.Resources>
    <Storyboard x:Key="OnMouseDown">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Red" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Border Name="border">
        <Border.Background>
            <LinearGradientBrush>
                <GradientStop Color="Red" Offset="0"/>
                <GradientStop Color="Green" Offset="0.5"/>
                <GradientStop Color="Blue" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Border.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown"
                          SourceName="border">
                <BeginStoryboard x:Name="MouseDown_BeginStoryboard"
                                 Storyboard="{StaticResource OnMouseDown}"/>
            </EventTrigger>
        </Border.Triggers>
    </Border>
</Grid>

更新

我也试图添加一个MouseLeave事件,但它仍在工作。

更新2

添加了MouseEnter

<Window.Resources>
    <Storyboard x:Key="OnMouseDown">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Red" />
    </Storyboard>
    <Storyboard x:Key="OnMouseLeave">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Blue" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Blue" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Blue" />
    </Storyboard>
    <Storyboard x:Key="OnMouseEnter">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Green" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Green" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Green" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Border Name="border">
        <Border.Background>
            <LinearGradientBrush>
                <GradientStop Color="Red" Offset="0"/>
                <GradientStop Color="Green" Offset="0.5"/>
                <GradientStop Color="Blue" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Border.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown" SourceName="border">
                <BeginStoryboard x:Name="MouseDown_BeginStoryboard" Storyboard="{StaticResource OnMouseDown}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave" SourceName="border">
                <BeginStoryboard x:Name="MouseLeave_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseEnter" SourceName="border">
                <BeginStoryboard x:Name="MouseEnter_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter}"/>
            </EventTrigger>
        </Border.Triggers>
    </Border>
</Grid>